Dictionary problem - AttributeError: 'int' object has no attribute 'get'

0

I'm starting to learn the basics in Python, and one of the examples on dictionary lists asks for something like this:

inventory = {'a' : 1 , 'b' : 6 , 'c' : 42 , 'd' : 1 , 'e' : 12}

def DisplayInventory(stuff):
    for k,v in stuff.items():
        stuff = k + v.get(stuff,0)
        print(k+v)

DisplayInventory(inventory)

But I still have this error:

Traceback (most recent call last):
   line 8, in <module>
    DisplayInventory(a)
   line 5, in DisplayInventory
    stuff = k + v.get(stuff,0)
AttributeError: 'int' object has no attribute 'get'    
    
asked by anonymous 29.12.2017 / 06:51

1 answer

1

The error happened because you tried to call a method that does not exist in int .

  

v.get(stuff,0)

As you later explained in the comments that you just wanted to show the key and value of each element in inventory , I was able to get to the following code from your

inventory = {'a' : 1 , 'b' : 6 , 'c' : 42 , 'd' : 1 , 'e' : 12}

def DisplayInventory(stuff):
    for k,v in stuff.items():
        print(str(k) + ' ' + str(v))

DisplayInventory(inventory)
    
29.12.2017 / 11:27