I am working on a function that allows a user to provide a list of values which will then autopopulate an if/elif block. For example, here is an example of the if/elif block that I am trying to autopopulate with acres_list:
acres_list = [15, 20, 25, 35, 45]
if acres <= 15:
return 1
elif 20 >= acres >15:
return 2
elif 25 >= acres > 20:
return 3
elif 35 >= acres > 25:
return 4
elif 45 >= acres > 35:
return 5
elif acres > 45:
return 6
I could provide a pointer to a specific list value like so:
acres = 17.46 # Some value to test. The function should return `2`
acres_list = [15, 20, 25, 35, 45]
def find_acres(acres, acres_list):
if acres <= acres_list[0]:
return 1
elif acres_list[1] >= acres >acres_list[0]:
return 2
elif acres_list[2] >= acres > acres_list[1]:
return 3
elif acres_list[3] >= acres > acres_list[2]:
return 4
elif acres_list[4] >= acres > acres_list[3]:
return 5
elif acres > acres_list[4]:
return 6
However, this would fail if the user provided a list such as:
acres_list = [15, 20, 25, 35, 45, 50, 60, 70, 120]
or
acres_list = [5, 10, 15]
What approach can I take to autopopulate an if/else block with a variable sized list of values?
Aucun commentaire:
Enregistrer un commentaire