How can I assign a value to x according to y

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kenbo

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
	enemy_health = 1
:bust_in_silhouette: Reply From: jgodfrey

The cleanest way to do this sort of thing with a function would require you to be able to calculate the resultingenemy_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.

:bust_in_silhouette: Reply From: Ninfur

The following code will set the health to a base value of 1 that increases by 1.5 every 10th wave

func add_health():
    enemy_health = 1 + (wave / 10) * 1.5

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…,

jgodfrey | 2022-04-08 21:18