# 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)
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?

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

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
``````