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

1 Like

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

Hello, can someone please help me out with this project. I am at the 7th step and just tested it with this code:
And their are a lot of errors popping up, can someone please let me know what I am doing wrong!

class School:
def init(self, name, level, numberOfStudents):
self.name = name
self.level = level
self.numberOfStudents = numberOfStudents
def name_getter(self):
return self.name
def level_getter(self):
return self.level
def numberOfStudents_getter(self):
return self.numberOfStudents
def numberOfStudents_setter(self, numberOfStudents_attribute):
self.numberOfStudents = numberOfStudents_attribute
def repr(self):
print("A {} school named {} with {} students ".format(self.level, self.name,self.numberOfStudents))

a = School(“Codecademy”, “high”, 100)
print(a)
print(a.name_getter())
print(a.level_getter())
a.NumberOfStudents_setter(200)
print(a.NumberOfStudents_getter())

You have some typos.
init should be __init__
.NumberOfStudents_setter should be .numberOfStudents_setter (first letter should not be capitalized)
.NumberOfStudents_getter should be .numberOfStudents_getter (first letter should not be capitalized)

class School: def __init__(self, name, level, numberOfStudents): self.name = name self.level = level self.numberOfStudents = numberOfStudents def name_getter(self): return self.name def level_getter(self): return self.level def numberOfStudents_getter(self): return self.numberOfStudents def numberOfStudents_setter(self, numberOfStudents_attribute): self.numberOfStudents = numberOfStudents_attribute def repr(self): print("A {} school named {} with {} students ".format(self.level, self.name,self.numberOfStudents)) a = School("Codecademy", "high", 100) print(a) print(a.name_getter()) print(a.level_getter()) a.numberOfStudents_setter(200) print(a.numberOfStudents_getter())

Thanks man, that really helped

class School: def __init__(self, name, level, numberOfStudents): self.name = name self.level = level self.numberOfStudents = numberOfStudents def name_getter(self): return self.name def level_getter(self): return self.level def numberOfStudents_getter(self): return self.numberOfStudents def numberOfStudents_setter(self, numberOfStudents_attribute): self.numberOfStudents = numberOfStudents_attribute def repr(self): print("A {} school named {} with {} students ".format(self.level, self.name,self.numberOfStudents)) a = School("Codecademy", "high", 100) print(a) print(a.name_getter()) print(a.level_getter()) a.numberOfStudents_setter(200) print(a.numberOfStudents_getter()) class PrimarySchool(School): def __init__(self, name, numberOfStudents , pickupPolicy): super().__init__(name, 'primary', numberOfStudents) self.pickupPolicy = pickupPolicy def pickupPolicy_getter(self): return self.pickupPolicy def __repr__(self): parentRepr = super().__repr__() return parentRepr + "The pickup policy is {}".format(pickupPolicy = self.pickupPolicy) b = PrimarySchool("Codecademy", 300, "Pickup Allowed") print(b.pickupPolicy_getter()) print(b)

I am up to section 14, any suggestions on what to do and why I am wrong?

In the __repr__ function, you don’t have anything in the {} that you se for the .format
so you shouldn’t have a named argument there.
change .format(pickupPolicy = self.pickupPolicy)
to .format(self.pickupPolicy)
to fix that.

or you could do
`return parentRepr + “The pickup policy is {pickupPolicy}”.format(pickupPolicy = self.pickupPolicy)

Also, a __repr__ method/function seems to be missing from the School class.

Please, find below the final code:

class School: def __init__(self, name, level, numberOfStudents): self.name = name self.level = level self.numberOfStudents = numberOfStudents def getName(self): return self.name def getLevel(self): return self.level def getNumberOfStudents(self): return self.numberOfStudents def setNumberOfStudents(self, newNumberOfStudents): self.numberOfStudents = newNumberOfStudents def __repr__(self): return "A {} school named {} with {} students".format(self.level, self.name, self.numberOfStudents) a = School("Codecademy", "high", 100) print(a) print(a.getName()) print(a.getLevel()) a.setNumberOfStudents(200) print(a.getNumberOfStudents()) class PrimarySchool(School): def __init__(self, name, numberOfStudents, pickupPolicy): super().__init__(name, 'primary', numberOfStudents) self.pickupPolicy = pickupPolicy def getPickupPolicy(self): return self.pickupPolicy def __repr__(self): parentRepr = super().__repr__() return parentRepr + " The pickup policy is {} after 3pm".format(self.pickupPolicy) b = PrimarySchool("Codecademy", 300, "Pickup Allowed") print(b.getPickupPolicy()) print(b) class HighSchool(School): def __init__(self, name, numberOfStudents, sportsTeams): super().__init__(name, 'high', numberOfStudents) self.sportsTeams = sportsTeams def getSportsTeams(self): return self.sportsTeams def __repr__(self): parentRepr = super().__repr__() return parentRepr + " The sports teams are {}".format(self.sportsTeams) c = HighSchool("Codecademy High", 500, ["Tennis", "Basketball"]) print(c.getSportsTeams()) print(c)

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

def getName(self):
return self.name

def getLevel(self):
return self.level

def getNumberOfStudents(self):
return self.numberOfStudents

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

def repr(self):
return “A {} school named {} with {} students”.format(self.level, self.name, self.numberOfStudents)

a = School(“Codecademy”, “high”, 100)
print(a)
print(a.getName())
print(a.getLevel())
a.setNumberOfStudents(200)
print(a.getNumberOfStudents())

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

def getPickupPolicy(self):
return self.pickupPolicy

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

b = PrimarySchool(“Codecademy”, 300, “Pickup Allowed”)
print(b.getPickupPolicy())
print(b)

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

def getSportsTeams(self):
return self.sportsTeams

def repr(self):
parentRepr = super().repr()
return parentRepr + " The sports teams are {}".format(self.sportsTeams)

c = HighSchool(“Codecademy High”, 500, [“Tennis”, “Basketball”])
print(c.getSportsTeams())
print(c)