Python 3 Intermediate - School Catalogue Project

Hello,

I’m running into an error with the Python 3 Intermediate School Catalogue Project that I’ve been unable to resolve. Specifically, when I call the repr for the School class, the string is returned successfully but then followed by a string error. Probably some silly mistake but I’ve been unable to figure it out. My code and an example of the call and error are below. Any suggestions would be appreciated!

Thanks

Tom

My code:

SCHOOL CATALOGUE PROJECT

class School:
def init(self, name, level, numberOfStudents):
self.name = name
self.level = level
self.numberOfStudents = numberOfStudents

define getters for certain properties

def get_name(self):
return self.name

def get_level(self):
return self.level

def get_numberOfStudents(self):
return self.numberOfStudents

define setter for numberOfStudents

def set_numberOfStudents(self,newNumberOfStudents):
self.numberOfStudents = newNumberOfStudents

create a repr() method to print School relevant info:

def repr(self):
print(‘A {level} school named {name} with {numberOfStudents} students’.format(level = self.level, name = self.name, numberOfStudents = self.numberOfStudents))

class PrimarySchool(School): # inherits from School
def init(self, name, numberOfStudents, pickupPolicy):
super().init(name, ‘primary’, numberOfStudents)
self.pickupPolicy = pickupPolicy

def get_pickupPolicy(self):
return self.pickupPolicy

def repr(self):
parentRepr = super().repr() # override parent repr method
print(parentRepr + “The pickup policy is {pickupPolicy}”.format(pickupPolicy = self.pickupPolicy))

class HighSchool(School): # inherits from School
def init(self, name, numberOfStudents, sportsTeams):
super().init(name,‘HighSchool’,numberOfStudents)
self.sportsTeams = sportsTeams

def get_sportsTeams(self):
return self.sportsTeams

def repr(self):
print(self.sportsTeams)

VERIFY FUNCTIONS

create school object and verify

mySchool = School(“Codecademy”,“high”,100)
print(mySchool) # prints repr stuff - not sure why error?

My error:

A high school named Codecademy with 100 students
Traceback (most recent call last):
File “script.py”, line 57, in
print(mySchool) # prints repr stuff - not sure why error after prints!
TypeError: str returned non-string (type NoneType)

Hello there!
repr is supposed to return a string which it’ll then print out.
You’re manually printing the string within the function but not returning anything.
It might not have given out an error for the school class, but in the primary class, parentRepr is getting an undefined value when you call super(), hence the error message.

Hope that helps

Hello,

Thank you for the advice, that totally makes sense. Code now working perfectly!

Cheers

Tom

Hi Tom,

I got stuck on the same exact part you did. However, unlike you, I learn only from examples, so the advice given by user ‘pluginmaybe’ sounds logical but no way I could produce the answer (there was only 1 example of repr in the lessons, google search didn’t help much either).

Any chance you could paste your correct answer in here?

Thanks,
Mike

Hi,
What they needed to do was basically swap the print() for return, so it’d become;

def __repr__(self):
  return "A {level} school named {name} with {numberOfStudents} students".format(level = self.level, name = self.name, numberOfStudents = self.numberOfStudents)

<and>

def __repr__(self):
   parentRepr = super().__repr__()
   return parentRepr + "The pickup policy is {pickupPolicy}".format(pickupPolicy = self.pickupPolicy)

The issue they were getting was because in the second repr, you have;
parentRepr = super().repr()
This is asking for a return value from the parent class. But, with it being a print statement, it wasn’t actually returning anything.

Hope that’s a bit clearer

1 Like

Much clearer! :+1: Thank you very much :smiley:

1 Like

I think I found multiple errors in the Codecademy lessons, for ‘Intermediate Python3’ for this project
All the steps that say ‘You should probably test…’ (so steps #7, #14 and #16) give incorrect syntax. For example, step #7 gives this example:

mySchool = School("Codecademy", "high", 100) print(mySchool) print(mySchool.getName()) print(mySchool.getLevel()) mySchool.setNumberOfStudents(200) print(mySchool.getNumberOfStudents()) ```

which produces this error message:

Traceback (most recent call last): File "script.py", line 29, in <module> print(mySchool.getName()) AttributeError: 'School' object has no attribute 'getName'

I changed the code and no error messages with the following:

mySchool = School("Codecademy", "high", 100) print(mySchool) print(mySchool.get_name()) print(mySchool.get_level()) mySchool.set_numberOfStudents(200) print(mySchool.get_numberOfStudents())

so it needed an underscore in between ‘get_name’, ‘set_numberOfStudents’, etc., and ‘name’ should not have been capitalized.

This produced a correct result an no error messages, but if I’m incorrect, please let me know.
Mike