So I'm trying to sum up the 'y' values of this code, so I can use it later on. Everything I've tried so far (a lot) didn't seem to work. And I just don't know how to fix it. This is my code:
inventory = {
'x' : {'x': 1, 'y': 2, 'z': 3},
'y' : {'x': 4, 'y': 5, 'z': 6}}
for key in inventory:
print(sum(inventory[key]['y']))
And this is the error code I get:
Traceback (most recent call last):
File "/home/runner/School-project/main.py", line 6, in <module>
print(sum(inventory[key]['y']))
TypeError: 'int' object is not iterable
I tried many things, which I don't even remember at this point to be honest. But if somebody could help it would be great!
You were super close!
You were never actually adding any values together, but you had the right idea. The sum() function expects an iterable like a list, and you were trying to use it with a raw integer.
This line:
Returns the each value with the 'y' key, and the values you have are integers.