+ 1
There are two lists of objects, the first one is a list of the pets(their name,age,gender), the second is the owners’ list.
How do i give an owner a pet? Meaning how do i for example link the first person in the owners’ list with the first pet in the pets’ list?Also remember that i have already made separate classes for each kind of pet,ie cats,birds,hamsters. Meaning the pets list can contain any kind of pet. https://code.sololearn.com/c1PBlh8mYYRg/?ref=app
20 Answers
+ 2
nested = {"Owner":
{"animal": "Cat",
"Gender":"Female",
"Age": 3},
"Owner2": {
"animal": "Dog"}}
print("owner1 = ", nested["Owner"])
print("owner2= ", nested["Owner2"])
Nested dictionary can look something like this you can easily update
Up to you to learn more about it
+ 1
Jay Matthews attribute error
0
Jay Matthews what if i want the fist person in owners’ list to have the second pet in the pets’ list?
0
I should be able to manipulate who is going to own which pet
0
Create a nested dictionary
0
Jay Matthews to print out all the contents in l is it enough to print(l)?
0
Ion Kare the classes must be used in this case, owner1 for example owns a class ”cat” and the cat’s name, gender and age should be included in that list
0
Ion Kare how about:
A = {owner(”john”, ”doe”): cat(”catname”, ”male”, 4}
0
#A = {owner:(”john”, ”doe”)}, cat:{”catname”, ”male”, 4}
#print(A)
A = {"owner":
{"first": "john",
"last": "doe",
"cat":
{"catname": "swimmy",
"Gender": "Male"}},
# second owner
"owner2":{
"first": "tom",
"last": "mop"
}}
#print(f"{A['owner']}")
print(f"{A['owner']['cat']}")
print("\n\tfirst ^^^ | second vvv")
print(f"\n{A['owner2']}")
Could totally do something like that , its many ways you can solve this why not try jay way its easy and simple and less confusing
0
Jay Matthews wont do, it just says [[<__main__.owner object……
0
Ion Kare the ”owner” in the dictionary is not related to the class right?
0
Jay Matthews that just shows the owner
0
Jay Matthews Does l,n = [],[] create 2 lists at the same time?so i could have written l = [] then n = [] it would be the same as l,n = [],[] right?
0
Jay Matthews [],[] stands for lists though?
0
Jay Matthews What if the owner has many pets? Is it enough with print(a[0].name, a[0].lastname, a[1].name, a[1].gender, a[1].age)??
0
Jay Matthews like u said this adds owner1 and pet1 in first in n then later in l list
l,n=[],[]
n.append(owner1)
n.append(pet1)
l.append(n)
If i were to add another owner (owner2) and pet(pet2) the same way would list ”l” look something like this:
l = [[owner1, pet1],[owner2,pet2]]?
0
Jay Matthews Ok.Assuming petlist is full:
Owner1 = owner(”Johnny”, ”Johnson”, pets[0])
ownerlist.append(owner1)
Print(ownerlist.name, ownerlist.lastname, ownerlist.pets)
Gives me a typeerror: __str__() missing a 3 required positional arguments
0
Jay Matthews Even the loop has to be inside the def __str__() part?
0
Jay Matthews
def __str__(self,name,Lastname,pets):
print(self.name, self.gender, self.age)
For a in pets:
print(a.name, a.gender, a.age)
This one wont work, i got the same output as last time
0
Jay Matthews i updated the code if thats what u meant