I am quite new to Python and I am struggling with a simple code that creates dictionaries and tries to print the information inside it. I created a dictionary named pets that contains the name of the pets as keys. The values are dictionaries that contain pieces of information about the pets.
I want to print in one sentence the name of the pet, and the information about the pet.
As shown in my code below, I am trying to create a for loop that will print a sentence for each pet.
pets = {
'maxi': {
'owner':'Laura',
'favorite food':'wiskas'
},
'chester': {
'owner':'Emilia',
'favorite food':'lula'
}
}
print(pets)
for name,info in pets.items():
print(name + "'s owner and favorite food are " + name['owner'] + ' and ' + name['favorite food'])
I get the following error:
Traceback (most recent call last): File "pets.py", line 14, in print(name + "'s owner and favorite food are " + name['owner'] + ' and ' + name ['favorite food']) TypeError: string indices must be integers