-1

The code

ralph = {'type': 'cat','owner': 'mark'}
lucy = {'type': 'cat','owner': 'carrie'}
pets = [ralph, lucy]

for pet in pets:
    print(str(pet))
    owner = pet['owner']
    print('pet type: ' + pet['type'] + '\n' + 'owner: ' + owner.title())

provides the output:

{'type': 'cat', 'owner': 'mark'}
pet type: cat
owner: Mark
{'type': 'cat', 'owner': 'carrie'}
pet type: cat
owner: Carrie

I was trying to print the name of each pet (Ralph and Lucy)

1 Answers1

0

You are trying to access the variable name. While this is possible in principle, you do not want to do this.

I assume you are trying somethin like this:

pet1 = {'type': 'cat','owner': 'mark', 'name': 'ralph'}
pet2 = {'type': 'cat','owner': 'carrie', 'name': 'lucy'}
pets = [pet1, pet2]

for pet in pets:
    print('pet name: ' + pet['name'])

Lydia van Dyke
  • 2,466
  • 3
  • 13
  • 25