School Catalogue python course

Hi there,

I’ve completed OOP School Catalogue. and I would like to see if I managed to completed up to standards?

Am I missing out on @decorators , setters, getters, del ? or I have completed this little project alright?

Github Gist

class School: def __init__(self, name, level, numberOfStudent): self.name = name self.level = level self.numberOfStudent = numberOfStudent def get_name(self): return self.name def get_level(self): return self.level def get_numberOfStudents(self): return self.numberOfStudent def set_numberOfStudents(self, new_amount): self.numberOfStudents = new_amount def __repr__(self): return f"A {self.level} school name {self.name} with {self.numberOfStudent} students." class PrimarySchool(School): def __init__(self, name, numberOfStudent, pickuppolicy): super().__init__(name, 'primary', numberOfStudent) self.pickuppolicy = pickuppolicy def get_policy(self): return self.pickuppolicy def __repr__(self): parentRepr = super().__repr__() return parentRepr + "The pickup polity is {pickupPolicy}".format(pickupPolicy = self.pickuppolicy) mySchool = School("Codecademy", "high", 100) print(mySchool) print(mySchool.get_name()) print(mySchool.get_level()) mySchool.set_numberOfStudents(200) print(mySchool.get_numberOfStudents()) print('\n') testSchool = PrimarySchool("Codecademy", 300, "Pickup Allowed") print(testSchool.get_policy()) print(testSchool) print('\n') class HighSchool(School): def __init__(self, name, numberOfStudent, sportsTeams): super().__init__(name, 'High', numberOfStudent) self.sportsTeams = sportsTeams def getSportsTeams(self): return self.sportsTeams def __repr__(self): parent = super().__repr__() return parent + f" information of our sport Team {self.sportsTeams}" c = HighSchool("Codecademy High", 500, ["Tennis", "Basketball"]) print(c)
3 Likes

Hi, @fiko_94

Your code looks great!

One small tip, though. In repr of your HighSchool class you are returning a string with a list of sports teams with the brackets, so the print message looks a little bit bulky. I would suggest to add .join method to uncover the brackets so to speak, like so:

 def __repr__(self):
    parent = super().__repr__()
    return parent + f" information of our sport Team {', '.join(self.sportsTeams)}"

A High school name Codecademy High with 500 students. information of our sport Team Tennis, Basketball

2 Likes

Thank you for your suggestion; once, I wanted to join the elements of an iterable and make it a string, but I completely forgot about the join method. Moreover, you even gave an example of its application, so much appreciation for it since I am just a beginner. I sometimes try to use the PhdEssay review service to read more texts and essay examples about Phyton, and essayservicescanner.com helps me indeed, but it is so hard to apply all the gotten knowledge. I would appreciate it if you could give me some more tips or tasks like homework to train my skills. Now, I am going to continue my programming studies.

hi there guys, thanks for useful topic

This is exactly what I was looking for, thank you so much.

When I finished with all the tasks, I thought that including a list there looks very ugly. And I have been googling how to convert elements of a list into normal string, but nothing I have tried worked. so far.

So how is your method different from using .format()? And why is yours better?

For reference, I am trying to use this:

def __repr__(self):
    parentrepr = super().__repr__()
    return parentrepr + 'The {name} sports teams are {sportsteams}.'.format(name = self.name, sportsteams = self.sportsteams)

So if possible I would like to keep the name of the school in the string to keep it looking fancy.
So I am trying to understand how that could work with combining your solution and my version, please?

I replied too hastily for the first time, as I have been able to figure it out.

I am just learning right now how to use f"string {yadda, yadda}", and what I have struggled with was realizing that using the delimiter with clipklop’s method means that I have to use double quotes for my strings and not single quotes.

But I have fixed that, so the final solution, which looks very fancy, looks like this:

def __repr__(self):
    parentrepr = super().__repr__()
    return parentrepr + f"The{self.name}" + f"sportsteams are {', '.join(self.sportsteams)}"

And this prints:
“A high school named Codecademy High with 500 students. TheCodecademy Highsportsteams are Tennis, Basketball, Counter Strike”

I took it even further to make it look normal and swapped them to be lower case.

which is obiously chucking a .lower() in the end

def __repr__(self):
    parentrepr = super().__repr__()
    return parentrepr + f"The{self.name}" + f"sportsteams are {', '.join(self.sportsteams)}".lower()

Hi, @janosspaits
I’m glad that you’ve figured it out!

Regarding your question, there’s not much difference between f-string and str.format() method as I know, f-string is just a newer way to put variables into the strings and probably more concise way to do it, but its all mater of taste :slight_smile:
If you still want to use .format method you could come up with something like this:

"The {0} sportsteams are {1}, {2}, {3}".format(self.name, *self.sportsteams)

note, that for each unpacking element of the list you’ve to put curly brackets placeholders beforehand

I like that, however it looks like it only works if there are 3 different sports teams. If there are less than 3, it gives an error, if it’s more than 3 it does not print the other teams. Or am I missing something ? :slight_smile:

Hey,
Yes, it’s true, you’ve to put exactly same amount of curly brackets as you have indexes in the list and this is a big downside of the .format method in this case. But there’s still a workaround for this with a little bit of ‘hack’ :smiley:

("The sportsteams are " + "{}, "*len(self.sportsteams)).format(*self.sportsteams)

That’s very smart, thanks for that! :slight_smile: