Home > OS >  printing a string in object oriented programming
printing a string in object oriented programming

Time:05-19

I'm working on a MOOC on Python Programming and am having a hard time finding a solution to a problem set. I hope you can provide some assistance.

The problem statement is: This problem uses the same Pet, Owner, and Name classes from the previous problem.

In this one, instead of printing a string that lists a single pet's owner, you will print a string that lists all of a single owner's pets.

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 = []

Add your get_pets_string function here!

Here's my code:

def get_pets_string(Owner):
    result = Owner.name.first   " "   Owner.name.last   "'s"   " "   "pets are:"    Pet.name
    return result

My code is getting the following error:

AttributeError: type object 'Pet' has no attribute 'name'
Command exited with non-zero status 1

Below are some lines of code that will test your function. You can change the value of the variable(s) to test your function with different inputs.

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))

Could you please offer some guidance on what I'm not doing right with my code?

CodePudding user response:

In your code, the name is an instance variable of Pet class. So, to access name of Pet you need an instance of Pet class. But in your code in the Pet.name, the Pet refers to the class and as there is no class variable name in Pet class, the above error is displayed.

To fix this, you can use the member pets of Owner class representing list of Pet object. So in the get_pets_string() you can iterate over pets member of Owner and print names of all the pets.

So after change to get_pets_string(), it will look like -

def get_pets_string(owner):
    result = owner.name.first   " "   owner.name.last   "'s pets are: "    ", ".join(p.name.first   " "   p.name.last for p in owner.pets)
    return result

Here I have used join() to show the name of all the pets separated by comma

  • Related