+ 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

24th Mar 2022, 1:29 AM
Lenoname
20 Respuestas
+ 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
24th Mar 2022, 2:34 AM
Ion Kare
Ion Kare - avatar
+ 1
Jay Matthews attribute error
24th Mar 2022, 9:12 PM
Lenoname
0
Jay Matthews what if i want the fist person in owners’ list to have the second pet in the pets’ list?
24th Mar 2022, 2:15 AM
Lenoname
0
I should be able to manipulate who is going to own which pet
24th Mar 2022, 2:16 AM
Lenoname
0
Create a nested dictionary
24th Mar 2022, 2:22 AM
Ion Kare
Ion Kare - avatar
0
Jay Matthews to print out all the contents in l is it enough to print(l)?
24th Mar 2022, 4:55 PM
Lenoname
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
24th Mar 2022, 5:17 PM
Lenoname
0
Ion Kare how about: A = {owner(”john”, ”doe”): cat(”catname”, ”male”, 4}
24th Mar 2022, 6:09 PM
Lenoname
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
24th Mar 2022, 8:40 PM
Ion Kare
Ion Kare - avatar
0
Jay Matthews wont do, it just says [[<__main__.owner object……
24th Mar 2022, 8:47 PM
Lenoname
0
Ion Kare the ”owner” in the dictionary is not related to the class right?
24th Mar 2022, 8:57 PM
Lenoname
0
Jay Matthews that just shows the owner
25th Mar 2022, 12:23 AM
Lenoname
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?
25th Mar 2022, 7:36 PM
Lenoname
0
Jay Matthews [],[] stands for lists though?
25th Mar 2022, 9:00 PM
Lenoname
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)??
25th Mar 2022, 9:13 PM
Lenoname
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]]?
25th Mar 2022, 11:16 PM
Lenoname
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
26th Mar 2022, 2:51 AM
Lenoname
0
Jay Matthews Even the loop has to be inside the def __str__() part?
26th Mar 2022, 4:04 AM
Lenoname
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
26th Mar 2022, 7:20 AM
Lenoname
0
Jay Matthews i updated the code if thats what u meant
26th Mar 2022, 7:52 AM
Lenoname