Delete a Key - how to use functions in this exercise

Hi, I was doing the lesson linked above and I was wondering how I could utilize functions for this. I used the following code
available_items = {“health potion”: 10, “cake of the cure”: 5, “green elixir”: 20, “strength sandwich”: 25, “stamina grains”: 15, “power stew”: 30}

health_points = 20

def add_health(item):

health_points += available_items.pop(item, 0)

return health_points

add_health(“stamina grains”)
add_health(“power stew”)

I got the error

Traceback (most recent call last):
File “”, line 9, in
add_health(“stamina grains”)
File “”, line 6, in add_health
health_points += available_items.pop(item, 0)
UnboundLocalError: local variable ‘health_points’ referenced before assignment

I was wondering, how could I do this in a way which does not give me this error.
Thanks in advance!

Often the error message gives us clues, even if the terminology is strange.

UnboundLocalError: local variable 'health_points' referenced before assignment

While the variable is visible and accessible for reading, it is not accessible for writing from another scope.

1 Like