0

Task: Define a member method print_all() for class PetData. Make use of the base class' print_all() method. Sample output for the given program with inputs: 'Fluffy' 5 4444 Name: Fluffy Age: 5 ID: 4444

The first error was that there was no newline after ID: 4444. When I fixed that, I started getting the word NONE before ID:. If I knew where it was coming from I could probably figure it out, but I've got nothing. PLEASE HELP!!!

class AnimalData:
    def __init__(self):
        self.full_name = ''
        self.age_years = 0

    def set_name(self, given_name):
        self.full_name = given_name

    def set_age(self, num_years):
        self.age_years = num_years

    # Other parts omitted

    def print_all(self):
        print('Name:',  self.full_name)
        print('Age:', self.age_years)


class PetData(AnimalData):
    def __init__(self):
        AnimalData.__init__(self)
        self.id_num = 0

    def set_id(self, pet_id):
        self.id_num = pet_id

    # FIXME: Add print_all() member method

""" Your solution goes here"""

user_pet = PetData()
user_pet.set_name(input())
user_pet.set_age(int(input()))
user_pet.set_id(int(input()))
user_pet.print_all()```


I've been working on this assignment for 2 days and I'm getting errors no matter what I try.  At one point the word NONE was randomly appearing before ID: 4444, still can't figure out where it's coming from.  PLEASE HELP!!!! 

Here is some of what I've tried and the errors I got.

## 
No newline after ID: 4444
def print_all(self):
AnimalData.print_all(self)
print('ID:', self.pet_id)

## 
ID number not showing in output.
def print_all(self):
print(AnimalData.print_all(self), 'ID:', self.id_num)

## 
No newline after ID: 4444
user_pet = PetData()
user_pet.set_name("Fluffy")
user_pet.set_age(5)
user_pet.set_id(4444)
user_pet.print_all()
tranese
  • 1
  • 1
  • 5
  • 1
    With correct indentation, your first attempt should work fine. – chepner Jul 24 '23 at 15:07
  • Why do you say there is no newline after the ID line? That call to `print` absolutely outputs a newline. – chepner Jul 24 '23 at 15:09
  • Does this answer your question? [How do I call a parent class's method from a child class in Python?](https://stackoverflow.com/questions/805066/how-do-i-call-a-parent-classs-method-from-a-child-class-in-python) – JonSG Jul 24 '23 at 15:16
  • @chepner those were the errors that zyBooks were giving. when I went back to check the indentation i noticed the issue. I had pet_id instead of id_num. Thanks a bunch! – tranese Jul 24 '23 at 15:27
  • @JonSG not even close, but thanks! – tranese Jul 25 '23 at 11:28
  • While correcting `pet_id` makes your code work, I don't believe you really want to call `AnimalData.print_all(self)` but rather `super().print_all()` as your class derives from `AnimalData`. – JonSG Jul 25 '23 at 14:10

1 Answers1

0

The correct answer is:

def print_all(self):
    AnimalData.print_all(self)
    print('ID:', self.id_num)

Thanks for your help @chepner!!

tranese
  • 1
  • 1
  • 5