Why cant I add values to a dictionary in run time!

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

I for the life of me cant add values to a dictionary its absolutely terrible as to why I cant add a simple function, I do dict[New_value] = Number that works fine but when I try to add another one it over writes it, why is it overwriting it, I didnt tell it to do that, why!!!

 func calculate_stats(new_value):
    	level = new_value
    	stats[hp_max] = ((float(species.Base_hp * level) / 50.0) + 5.0)
    	stats[attack] = ((float(species.Base_attack * level) / 100.0) + 5.0)
    	stats[special_attack] = ((float(species.Base_special_attack * level) / 100.0) + 5.0)
    	stats[defence] = ((float(species.Base_defence * level) / 100.0) + 5.0)
    	stats[special_defence] = ((float(species.Base_special_defence * level) / 100.0) + 5.0)
    	stats[speed] = ((float(species.Base_speed * level) / 100.0) + 5.0)
    	print(stats[hp_max])
    	print(stats[attack])
    	print(stats)
	

Heres a function that I set the values to the dictionary, its doing some damage calculations its doing the calculations correct but its not storing them correctly, I print the dictionay called stats at the end, and it only shows one value, how do I resolve this!!

:bust_in_silhouette: Reply From: Coxcopi

The Keys in the brackets need to be strings as far as I know, so e.g.

print(stats["hp_max"])

That was it! Me banging my head thinking the code doesn’t work , but it was simply because it wasn’t a string!

Dragon20C | 2021-05-31 22:08

:bust_in_silhouette: Reply From: PunchablePlushie

why is it overwriting it, I didnt tell it to do that,

I assume those hp_max, attack, etc are not variables and are string key names (AKA "hp_max", "attack"). Please correct me if I’m wrong here.

Anyways, the reason it’s overwriting stuff is because that’s how dictionaries work. When you use dictionary["key"] = value, it looks for that "key" in the dictionary. If it exists, it replaces its old value with the new value. If not, then it creates it.

So when you call calculate_stats() for the first time, it creates those keys (hp_max, attack, etc). After that, every time you call it, it simply replaces the value of those keys. It doesn’t create new keys.


If you’re looking for a way to calculate stats for each level and keep them separate, you can try nesting another dictionary in your dictionary for every level. Something like this:

stats = {
    "1": {"max_hp": 10, "attack": 10"}.
    "2": {"max_hp": 12, "attack": 15"}.
    "3": {"max_hp": 14, "attack": 20"}.
}

The code for that could look something like this:

 func calculate_stats(level):
     stats[level] = {}
     stats[level][hp_max] = 0
     stats[level][attack] = 0
     # Other stats here

Thanks for the comment, those values are supposed to be strings apparently, and they didn’t have any value applied to them so it was replacing the same value, simply making them a string fixes this very annoying issue!

Dragon20C | 2021-05-31 22:10