0

This is my desired output:

Name: Smith, Age: 20, ID: 9999

Here is my code so far

class PersonData:
def __init__(self):
    self.last_name = ''
    self.age_years = 0

def set_name(self, user_name):
    self.last_name  = user_name

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

# Other parts omitted

def print_all(self):
    output_str = 'Name: ' + self.last_name + ', Age: ' + str(self.age_years)
    return output_str


class StudentData(PersonData):
def __init__(self):
    PersonData.__init__(self)  # Call base class constructor
    self.id_num = 0

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

def get_id(self):
    return self.id_num


course_student = StudentData()

course_student = StudentData()
course_student.get_id(9999)
course_student.set_age(20)
course_student.set_name("Smith")

print('%s, ID: %s' % (course_student.print_all(), course_student.get_id()))

At the moment, it isn't running. I would really appreciate it if someone could help out. It is returning a type error for line 34, and I am not sure how to correct it. Any help would be greatly appreciated.

3
  • 1
    Please post the full error message. One thing I noticed is you're calling get_id where you should be calling set_id Commented Nov 5, 2019 at 0:43
  • Including the text of the type error would help, but I imagine it's happening because you called get_id(9999) instead of set_id(9999). Commented Nov 5, 2019 at 0:43
  • You're passing 9999 into get_id, but get_id doesnt take any arguments Commented Nov 5, 2019 at 0:43

2 Answers 2

1

You're invoking the parent init wrongly there...

Here's how you're supposed to do it:

class PersonData:
    def __init__(self):
        self.last_name = ''
        self.age_years = 0

    def set_name(self, user_name):
        self.last_name  = user_name

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

    # Other parts omitted

    def print_all(self):
        output_str = 'Name: ' + self.last_name + ', Age: ' + str(self.age_years)
        return output_str


class StudentData(PersonData):
    def __init__(self):
        super().__init__()  # Call base class constructor
        self.id_num = 0

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

    def get_id(self):
        return self.id_num


course_student = StudentData()

course_student = StudentData()
course_student.set_id(9999)
course_student.set_age(20)
course_student.set_name("Smith")

print('%s, ID: %s' % (course_student.print_all(), course_student.get_id()))

I also noticed that in the execution, you were calling course_student.get_id(9999) but I think you meant course_student.set_id(9999)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for explaining that! It was giving me trouble for a while
Don't forget to mark the response as the answer so others can learn from it too
0

here's the correct way to pass all test using ZyBooks

class PersonData:
    def __init__(self):
        self.last_name = ''
        self.age_years = 0

    def set_name(self, user_name):
        self.last_name  = user_name

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

    # Other parts omitted

    def print_all(self):
        output_str = 'Name: ' + self.last_name + ', Age: ' + 
        str(self.age_years)
        return output_str


class StudentData(PersonData):
    def __init__(self):
        PersonData.__init__(self)  # Call base class constructor
        self.id_num = 0

    def set_id(self, student_id):
        self.id_num = student_id
        
    def get_id(self):
        return self.id_num


course_student = StudentData()
course_student.set_name('Smith')
course_student.set_age(20)
course_student.set_id(9999)

print('{}, ID: {}'.format(course_student.print_all(), 
course_student.get_id()))

be sure you dont have the line course_student = StudentData( ) coded twice in your code because then your instructor will know that you copied it and pasted it from another website.

1 Comment

This code is full of IndentationErrors. Please edit it and fix them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.