I am trying to work on this first part of the project but I keep getting error messages. Does anyone know why?
1 Like
I was able to figure this much out but can someone explain why f is necessary in the statement found in repr.
vic-st
September 7, 2022, 5:04pm
3
By adding the letter f, you can use string interpolation. It lets Python know that you don’t want the values between the curly braces to be treated like text
1 Like
Thank you very much. I have a follow-up question: Why doesn’t the letter f work in this instance with print.
vic-st
September 7, 2022, 5:07pm
5
It does! Just switch from single quotes to double quotes:
1 Like
Awesome thank you so much! I typically use single quotes for strings but I should get into the habit of using double quotes.
1 Like
vic-st
September 7, 2022, 5:13pm
7
It’s a hard habit to break especially when some languages really want you to use single quotes half the time
There has to be something else to this. There is no requirement for single or double quotes when using f-strings.
Just noticed the issue. The __repr__
method must return a string. print
is not return
.
2 Likes
vic-st
September 7, 2022, 5:42pm
9
Woooah, good point, totally overlooked that
2 Likes
My final code for the SchoolCatalogue Project if anyone needs it for reference.
print('Hello world!')
class School:
def __init__(self, name, level, number):
self.name = name
self.level = level
self.number = number
def get_name(self):
return self.name
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 f'A {self.level} school named {self.name} with {self.number} students.'
a = School("Codecademy", "high", 100)
print(a)
print(a.get_name())
print(a.get_level())
a.set_number(200)
print(a.get_number())
class PrimarySchool(School):
def __init__(self, name, number, pickup):
super().__init__(name, 'primary', number)
self.pickup = pickup
def get_pickup(self):
return self.pickup
def __repr__(self):
primaryRepr = super().__repr__()
return primaryRepr + f'The pickup policy is {self.pickup}'
b = PrimarySchool("Codecademy", 300, "Pickup Allowed")
print(b.get_pickup())
print(b)
class HighSchool(School):
def __init__(self, name, number, sports):
super().__init__(name, 'high', number)
self.sports = sports
def get_sports(self):
return self.sports
def __repr__(self):
highRepr = super().__repr__()
return highRepr + f"You can play the following sports: {self.sports}."
c = HighSchool("Codecademy High", 500, ["Tennis", "Basketball"])
print(c.get_sports())
print(c)
3 Likes
Thank you! They took the training wheels off before I was ready!
1 Like