Home > database >  How to return a list called in a class in python
How to return a list called in a class in python

Time:06-12

Struggling with this:

Write a function called get_pets_string. get_pets_string should have one parameter, an instance of Owner. get_pets_string should return a list of that owner's pets according to the following format:

David Joyner's pets are: Boggle Joyner, Artemis Joyner
class Name:
    def __init__(self, first, last):
        self.first = first
        self.last = last

class Pet:
    def __init__(self, name, owner):
        self.name = name
        self.owner = owner
        
class Owner:
    def __init__(self, name):
        self.name = name
        self.pets = []

If your function works correctly, this will originally print:

David Joyner's pets are: Boggle Joyner, Artemis Joyner
Audrey Hepburn's pets are: Pippin Hepburn
owner_1 = Owner(Name("David", "Joyner"))
owner_2 = Owner(Name("Audrey", "Hepburn"))

pet_1 = Pet(Name("Boggle", "Joyner"), owner_1)
pet_2 = Pet(Name("Artemis", "Joyner"), owner_1)
pet_3 = Pet(Name("Pippin", "Hepburn"), owner_2)

owner_1.pets.append(pet_1)
owner_1.pets.append(pet_2)
owner_2.pets.append(pet_3)

print(get_pets_string(owner_1))
print(get_pets_string(owner_2))

Here is my code as below:

def get_pets_string(owner):
    for obj in owner.pets:
       return owner.name.first   " "   owner.name.last   "'s pets are: "   obj.name.first   " "   obj.name.last

My answer can only print one pet for each owner like this:

David Joyner's pets are: Boggle Joyner
Audrey Hepburn's pets are: Pippin Hepburn

CodePudding user response:

You can implement a quick helper function to change how the string is displayed and simply displaying the name:

def repr_name(name):
    return name.first   " "   name.last

Then you can display all the pets together:

def get_pets_string(owner):
   return f"{repr_name(owner.name)}'s pets are: {', '.join([repr_name(pet.name) for pet in owner.pets])}"

The ', '.join([str(pet.name) for pet in owner.pets]) puts the name of every pet into the list, and then separates each of them with commas.

CodePudding user response:

You can use str.join on a generator expression that yields formatted strings.

', '.join(f"{pet.name.first} {pet.name.first}" for pet in owner_1.pets)

This yields:

'Boggle Boggle, Artemis Artemis'

As this is homework, I don't want to give you the whole solution, so I'll leave it to you to put this piece together with the rest.

CodePudding user response:

The issue is that your function is returning the value. Return implies the end of the function, so its not iterating through the other pets.

The function should be like the follow...

def get_pets_string(owner):
    owner_string = f"{owner.name.first} {owner.name.last}'s pets are: "
    
    pets_string = ""
    for obj in owner.pets:
        pet_name = f"{obj.name.first} {obj.name.last}"
        pets_string  = pet_name   " "
    
    return owner_string   pets_string

You can actually refactor your code by returning fullname as methon on each class

CodePudding user response:

the problem is that you're returning immediately in your first iteration

def get_pets_string(owner):
    for obj in owner.pets:
       return owner.name.first   " "   owner.name.last   "'s pets are: "   obj.name.first   " "   obj.name.last
        #^ here is the problem, you need to iterate over your whole list but this stop after the first iteration

You need to construct your result cumulatively, like so

def get_pets_string(owner):
    owner_name = owner.name.first   " "   owner.name.last   "'s pets are: "
    pets = []
    for obj in owner.pets:
       pets.append(obj.name.first   " "   obj.name.last) #gather all the pets names in a list
    return  owner_name   ", ".join(pets) #use join to put together all the pets name we build previously

CodePudding user response:

Thank you very much for all the replies above!! I do appreciate all of your input and knowledge. I did comment on every reply but I don't know why my comment disappear... So I wrote it here to thanks for everyone's help and advice!!

  • Related