0
How do i for example search for pet1 in ownerlist?
7 Answers
+ 1
Let me get back to you later, if no other came with an answer by then. I have to go now ...
0
Can't use <pet1> for searching, it's a variable representing a pet.
This can search by name, type or gender
def find_pet( owner_list, query_string, category ):
result = []
for o in owner_list:
for p in o.pets:
match = False
if category == 1:
match = p.name == query_string
elif category == 2:
match = p.typeofpet == query_string
elif category == 3:
match = p.gender == query_string
if match:
result.append( p )
return result
pet_name = 'roro' # search by name
pet_type = 'cat' # search by type
pet_gender = 'male' # search by gender
print( f"Find name {pet_name}" )
print( *find_pet( ownerlist, pet_name, 1 ) )
print( f"Find type {pet_type}" )
print( *find_pet( ownerlist, pet_type, 2 ) )
print( f"Find gender {pet_gender}" )
print( *find_pet( ownerlist, pet_gender, 3 ) )
0
Ipang can i search for petlist[0] in ownerlist instead?
0
Yes, first you change find_pet( ) to accept <name>, <type> and <gender> parameters, and make adjustments for qualifying <match>. And then you extract values for those arguments from <petlist[0]> to be used for calling find_pet( ).
0
Ipang Is that what you did?âŹïž
0
What is it exactly are you trying to find? a specific pet, or any pet placed as first element? I think I might have misunderstood you ...
0
Ipang Iâm gonna show the âuserâ a list of pets(petlist), he/she is going a to choose a pet based on their position in the list of pets. I then will look for that pet in the ownerlist and see who owns that specific pet