0

Pets: Make several dictionaries, where the name of each dictionary is the name of a pet. In each dictionary, include the kind of animal and the owner’s name. Store these dictionaries in a list called pets. Next, loop through your list and as you do print everything you know about each pet.

Hi i have this question. I have done the solution but i would like to print the dictionaries name without writing print for each

solution:

jumbo = {"kind":"cat",
         "owner":"Rahma",
}
 


snoopy = {"kind":"dog",
          "owner":"fahad",
}


shilla = {"kind":"bird",
          "owner":"farooq",
}


pets = [jumbo,snoopy,shilla] # created list 

#loop through your list and as you do print everything you know about each pet

for pet in pets :
    print ( "jumbo" + " is "+ pet["kind"] + " the owner is " + pet["owner"].title() + "." ) )

The problem is i am trying to print the names in each loop using the list pets that was created

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Fahad
  • 45
  • 1
  • 7
  • Can you please rephrase your question? – Petr L. Aug 28 '21 at 13:10
  • 1
    Include the name of the pet in the pet's dictionary, as an extra field: `jumbo = { 'name': 'jumbo', "kind":"cat", "owner":"Rahma" }` – Stef Aug 28 '21 at 13:11
  • I got the question from a book so i can't rephrase it #Petr L – Fahad Aug 28 '21 at 13:13
  • jumbo = { 'name': 'jumbo', "kind":"cat", "owner":"Rahma" } this going to work perfectly . but is there another way of doing it ? I am learning that's why i am asking -stef – Fahad Aug 28 '21 at 13:16
  • https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string –  Aug 28 '21 at 13:23

2 Answers2

1

you can use eval() like below(see ref):

jumbo = {"kind":"cat",
         "owner":"Rahma",
}

snoopy = {"kind":"dog",
          "owner":"fahad",
}

shilla = {"kind":"bird",
          "owner":"farooq",
}

pets = ['jumbo','snoopy','shilla'] # created list 

for pet in pets :
    print ( (pet) + " is "+ str(eval(pet)["kind"]) + " the owner is " + str(eval(pet)["owner"]) + "." )

output:

jumbo is cat the owner is Rahma.
snoopy is dog the owner is fahad.
shilla is bird the owner is farooq.
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • I would not recommend this method in any serious program. The name of the variable and the name of the pet should not be dependent on each other. This is how we end up with spaghetti code. – Stef Aug 28 '21 at 13:21
  • @lachi007, welcome, please don't forget `accept` answer and `upvote` – I'mahdi Aug 28 '21 at 17:25
  • Hi thank you for the reminder . I will do that . – Fahad Aug 28 '21 at 17:40
1

Try this two solutions:

First - Zip with lists of pets name:

names = ["jumbo", "snoopy", "shilla"]

# loop through your list and as you do print everything you know about each pet

for name, pet in zip(names, pets):
    print(name + " is " + pet["kind"] + " the owner is " + pet["owner"].title() + ".")

Second - add the name to the dictionary:

jumbo = {"name": "jumbo", "kind": "cat",
         "owner": "Rahma",
         }

snoopy = {"name": "snoopy", "kind": "dog",
          "owner": "fahad",
          }

shilla = {"name": "shilla", "kind": "bird",
          "owner": "farooq",
          }
for pet in pets:
    print(pet["name"] + " is " + pet["kind"] + " the owner is " + pet["owner"].title() + ".")
Tomer S
  • 900
  • 1
  • 10
  • 21