My solution

However in the instructions it is said to write the instructions in a single line, I succeeded only in using two, this is my code, i hope it helps you!!

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
health_points+=available_items.get("stamina grains",0)
available_items.pop("stamina grains",0)
health_points+=available_items.get("power stew",0)
available_items.pop("power stew",0)
health_points+=available_items.get("mystic bread",0)
available_items.pop("mystic bread",0)
print(available_items)
print(health_points)
1 Like

Hey there, can you please explain the instructions? Or this lesson? I didn’t quite get it. Esp the part:

health_points += available_items.pop("stamina grains", 0)

the “+=” part, didn’t get it. Did I miss anything?

Thanks in advance!

The += operator is for in-place addition. It is designed to alter the existing value in-place where possible-

x = []
x += [1]
# behaves like the list.extend method

x = []
x.extend([1])
# in both cases we update the object and keep the same name, x

x = []
# This is NOT equivalent to-
x = x + [1]  # this makes a new object and assigns it to the same name

For an integer you cannot alter the integer object itself (integers are immutable in Python, this might not mean much now but it will in the future, have a web search for info). In this case the following behaviour is equivalent-

x = 3
x += 1
# x now references an integer 4
x = 3
x = x + 1
# this behaves the same as the above

So for this example you can treat it like health_points = health_points + number. Just be aware that mutable types like the list example above do not follow the same rules for += and will mutate the object itself.

1 Like

I think I wasn’t very clear with my question or I think I still am not getting it. Can you maybe explain the whole code above and its purpose? I didn’t understand this lesson instructions as well.

health_points = 20
health_points+=available_items.get("stamina grains",0)
available_items.pop("stamina grains",0)

How and where was it mentioned to put health_points equal to 20?
What purpose does the next line serve? If we don’t put that line, what’d happen, aside from the error maybe?

Could you please add a link to the lesson/project, it isn’t clear where this is from so I don’t actually know what the instructions are.

Q.1 I do not know
Q2. I believe available_items is a dictionary where the values are probably integers or floats. If you’ve not seen it before the dict.get- Built-in Types — Python 3.9.5 documentation method gets the value from a dictionary given a certain key. The second value (zero here) of this method is the default value should be returned if that key cannot be located in that dictionary. So you add either a certain value to health_points or 0 if the key given is not valid.

If you’re implementing this project yourself I’d suggest completing it first with topics you already know. It’s always tempting to search for a more efficient or clean solution but if you’re just learning then at the very least save this until after you solve the problem (part of what you learn from projects is general problem solving).

1 Like

Python Dictionaries | Codecademy

1 Like

Hmm as for Q1 I cannot say for sure, was it perhaps included in the default script when you started the lesson?

Nope. It wasn’t included. I think we can put any value for health_points.

I just tried resetting the lesson and wound up with-

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

Best to start with those values in this case.

2 Likes

All right! Thanks for helping out! : )

1 Like