# How can I assign a value to x according to y

Attention Topic was automatically imported from the old Question2Answer platform.

I have this code that is very annoying to write but does the job, I was wondering if there was a function or class of some sort to help me.

Here’s my function:

``````func add_health():
if wave in range(10,20):
enemy_health = 2
elif wave in range(20,30):
enemy_health = 3.5
elif wave in range(30,40):
enemy_health = 5
elif wave in range(40,50):
enemy_health = 6.5
elif wave in range(50,59):
enemy_health = 8
elif wave in range(60,69):
enemy_health = 9.5
else:
enemy_health = 1
``````

The cleanest way to do this sort of thing with a function would require you to be able to calculate the resulting`enemy_health` value from the incoming `wave` value. While that’s probably possible, the calculation isn’t immediately obvious to me - especially since the ending range of the last two items above is different than the previous items.

Short of some way to calculate the resulting `enemy_health` value, you could also create a dictionary containing the relevant data (`range_start`, `range_end`, and `result`), populate the dictionary with appropriate data, and then write a function that could compare an incoming `wave` value with the dictionary contents and return an appropriate result.

While that’s not a lot different than the raw function you show at the top, at least you’d only have to supply the 3 numbers for each case instead of writing code for each.

``````func add_health():
Yep, this is a good example of my first suggestion (calculate `enemy_health` based on an input `wave`). And, while this seems reasonable, notice that it will not return ALL of the same values shown in the OP due to some variance there…,