In this exercise, the pop() method is used to delete a key from a dictionary and the example shows that it is possible to specify a return value if the key isn’t present. Does an error occur if the key is not present and no default value is provided?
Answer
Yes, if the pop() method is called on a key that is not present in the dictionary and NO default value is provided in the pop() call, then the method will raise a KeyError exception.
the assignment requests to " In one line, add the corresponding value of the key "stamina grains" to the health_points variable and remove the item "stamina grains" from the dictionary. If the key does not exist, add 0 to health_points ."
Zero understanding how one can re-assign the value to the key while using .pop()
TIA for your piece of advice
The dict.pop() method takes one or two arguments, the first one is required, and must be a key. The second argument is a default return value when the specified key is not found to be present in the dictionary.
My understanding if print(available_items.pop(“stamina grains”, 0)) we should get 20 instead of 15. I do not understand how to achieve that
and this is the pop-up error message produced by CA at the bottom of the screen: " Using .pop() , add the value of 'stamina grains' to health_points and remove it."
Thank you for being so prompt - your time is greatly appreciated!!
I see what you did there.
The more I read that specific line (and next paragraphs below ), the more I get the assignment - I got confused between subject and object in the syntax of that text
It has to be 35 as you adding values to the variable
Thank you for clarifying that, and thank you for your time
I couldn’t get pass this exercise, i couldn’t able to figure out what is the issue in my code. Could any one help me resolve this issue? which will help me understand better and proceed further with the course, Thanks.
Instruction: You are designing the video game Big Rock Adventure. We have provided a dictionary of items that are in the player’s inventory which add points to their health meter. In one line, add the corresponding value of the key "stamina grains" to the health_points variable and remove the item "stamina grains" from the dictionary. If the key does not exist, add 0 to health_points.