Python 3 School Catalogue Model Answer

Python 3 School Catalogue Model Answer

Please tell me if there is a model answer or video supporting the Python 3 School Catalogue project. My code produces a result, but I suspect could be improved, especially in relation to inheritance (e.g. correct use of super())

I could not find either, but here is my solution

class School: def __init__(self, name, level, number): = name self.level = level self.number = number def get_name(self): return def get_level(self): return self.level def get_number(self): return self.number def set_number(self, new_number): self.number = new_number def __repr__(self): return str((f'A {self.level} school named {} with {self.number} students')) #TEST Oxford_School = School("Oxford", "High", 10000) print(Oxford_School) class PrimarySchool(School): def __init__(self, name, number, pickup_policy): super().__init__(name, "Primary", number) self.pickup_policy = pickup_policy def get_primary_school(self, new_value): self.pickup_policy = new_value def __repr__(self): return super().__repr__() Cambridge_School = PrimarySchool("Cambridge", 5000, "Pickup Allowed") print(Cambridge_School) class HighSchool(School): def __init__(self, name, number, sport_teams): super().__init__(name, "High School", number) self.sport_teams = sport_teams sport_teams = [] def get_sport_teams(self): return list(self.sport_teams) def __repr__(self): a = super().__repr__() return a + f' has {self.sport_teams} teams' Yildiz_School = HighSchool("Yildiz", 1000, ["Tennis", "Football" , "Volleyball"]) print(Yildiz_School)

Many thanks. Here is my effort (have not yet analysed where we differ):

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, new_numberOfStudents):
if isinstance(new_numberOfStudents, int):
self._numberOfStudents = new_numberOfStudents
raise TypeError

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


class PrimarySchool(School):
def init(self, name, level, numberOfStudents, pickupPolicy):
super().init(name, level, numberOfStudents)
self._name = name
self._level = “primary”
self._numberOfStudents = numberOfStudents
self.pickupPolicy = pickupPolicy

def getpickupPolicy(self):
return self.pickupPolicy

def repr(self):
return “{name}'s pick-up policy is {pickupPolicy}”.format(name = self._name, pickupPolicy = self.pickupPolicy)


class HighSchool(School):
def init(self, name, level, numberOfStudents, *sportsTeams):
super().init(name, level, numberOfStudents)
self._name = name
self._level = “primary”
self._numberOfStudents = numberOfStudents
self.sportsTeams = sportsTeams

def getsportsTeams(self):
return self.sportsTeams

def repr(self):
return “{name} has these sports teams: {sportsTeams}”.format(name = self._name, sportsTeams = self.sportsTeams)


School class tests

a = School(“St Cuthbert’s”, “High”, 25)
print (a)

print("name a: " + a.getName())
print("level a: " + str(a.getLevel()))
print("no of students after set a: " + str(a.getNumberOfStudents()))


PrimarySchool class tests

b = PrimarySchool(“Holmewood”, “Primary”, 56, “Pickup after 3pm”)
print (b)

print("Name b: " + b.getName())
print("level b: " + str(b.getLevel()))
print("pick-up policy b: " + str(b.getpickupPolicy()))
print("number of students b: " + str(b.getNumberOfStudents()))


HighSchool class tests

c = HighSchool(“Mill Hill”, “High”, 400, “rugby”, “hockey”, “cricket”)
print (c)

print("Name b: " + c.getName())
print("level b: " + str(c.getLevel()))
print("sports teams c: " + str(c.getsportsTeams()))
print("number of students c: " + str(c.getNumberOfStudents()))

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.