Create the School Class Python 3 intermediate

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):
    print( "A {self.level} school named {self.name} {self.numberOfStudents} students")

mySchool = School("Codecademy", "high", 100)
#print(mySchool)
#print(mySchool.getName())
#print(mySchool.getLevel())
#mySchool.setNumberOfStudents(200)
#print(mySchool.getNumberOfStudents())
class PrimarySchool(School):
  def __init__(self, name, numberOfStudents, pickupPolicy):
    super().__init__(name, "primary", numberOfStudents)
    self.pickupPolicy = pickupPolicy
  def getPickupPolicy(self, pickupPolicy):
    return self.pickupPolicy
  def __repr__(self):
    PrimarySchool = super().__repr__()
    return PrimarySchool + "The pickup policy is {pickupPolicy}".format(pickupPolicy = self.pickupPolicy)

testSchool = PrimarySchool("Codecademy", 300, "Pickup Allowed")
#print(testSchool.getPickupPolicy())
print(testSchool)


i am having some problems with my code here :slight_smile:
line 31, in repr
return PrimarySchool + “The pickup policy is {pickupPolicy}”.format(pickupPolicy = self.pickupPolicy)

TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

i followed the instructions but i just cannot figure it out the error, any help will very much appreciated it! i am 2 hrs here staring at it

Ta for changing the post. For that error at least the __repr__ and __str__ functions must return a string, so make sure to use return itself.

For a little detail on the error itself: because one of the parent classes uses print but never returns anything, the .__repr__ function itself will return None. When you try and combine the return of the parent class via super().__repr__ you’re trying to combine the returned None with a string, hence the specific error you’re getting.

As a final aside try to avoid shadowing the PrimarySchool class name inside the __repr__ function (don’t assign to the name of an existing class). It won’t actually make a difference in this example since it’s a limited scope but it’s not best practice.

1 Like

wow thanks for that, i did not think about it at all! i did the changes but now i went back to my previous error which was

line 34, in
print(testSchool.getPickupPolicy())

TypeError: getPickupPolicy() missing 1 required positional argument: ‘pickupPolicy’
would you know what positional argument is it pointing at?
when i undo the #print(testSchool.getPickupPolicy()) it comes up!!
also just one more question, i thought by calling
return "A {self.level} school named {self.name} {self.numberOfStudents}…
the {} would fill in automatically but that is not the case, i tried to to the format(self.level) for all of them with their respective variables but it just threw error
promise it will be the last question!! gosh that there is so much to learn, my poor head

That particular call testSchool.getPickupPolicy() comes from this definition aye, def getPickupPolicy(self, pickupPolicy):?

When called as an instance a reference to the instance object itself will be passed for the first argument (to parameter self) but you still have the pickupPolicy as a second unfilled parameter. This is your missing positional argument, you’ll need to either pass that argument or decide if you can remove that parameter from the call signature.

As for the string you might be thinking of the f-string expression formatting. You can use it like f"This is a string, this is a {variable}, more {object.data}".

It must have an f or F before the string, e.g. f"print {this}" for it to be interpreted this way.

1 Like

thank you so much!! really appreciate the explanation!! i still have many things to work on… really thanks

1 Like

A post was split to a new topic: Why simpler language can’t be used?


I am not sure how to fix this error. Please help!

You have an unnecessary parameter for the get_pickUpPolicy function/method.

The pickUpPolicy parameter should not be there.

It should be

  def get_pickUpPolicy(self):
    return self.pickUpPolicy

The error is because of the mismatch between the number of parameters other than self when the function is defined (1),
and the number of arguments when the function is called (0).


I am now getting a different error message after fixing the original issue of having a secondary argument.

It looks like the problem is in the PrimarySchool class.

What’s your code for the PrimarySchool class ?
Use the </> button and paste your code between the ``` and ```
not a screenshot, please.

Screenshot 2022-06-21 193859

Here’s code similar to what I had for the PrimarySchool class and it’s constructor/initializer

class PrimarySchool(School):
  def __init__(self, name, numberOfStudents, pickupPolicy):
    super().__init__(name, 'primary', numberOfStudents)
    self.pickupPolicy = pickupPolicy

Check if you set self.pickupPolicy to be the value of the appropriate argument in a similar way.

Alternatively, you could use the __ for the variable and have a getter and setter:

class PrimarySchool(School):
  def __init__(self, name, numberOfStudents, pickupPolicy = None):
    super().__init__(name, 'primary', numberOfStudents)
    self.__pickupPolicy = pickupPolicy
  
  @property  #@pickupPolicy.getter
  def pickupPolicy(self):
    return self.__pickupPolicy

  @pickupPolicy.setter
  def pickupPolicy(self, policy):
    self.__pickupPolicy = policy

I actually did the getters and setter differently on my project; I used Python’s property function.

class PrimarySchool(School):
  def __init__(self, name, numberOfStudents = 0, pickupPolicy = None):
    super().__init__(name, 'primary', numberOfStudents)
    self.__pickupPolicy = pickupPolicy
  
  def get_pickupPolicy(self):
    return self.__pickupPolicy

  def set_pickupPolicy(self, policy):
    self.__pickupPolicy = policy
  
  pickupPolicy = property(fget = get_pickupPolicy, fset = set_pickupPolicy)

I have what you have in the 1st example provided.

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

You have a spelling error in the __init__ function.

You wrote self.pickupPoilcy instead of self.pickupPolicy

It seems fine otherwise.

class School: def __init__(self, name, level, numberOfStudents): self.name = name self.level = level self.numberOfStudents = numberOfStudents def __repr__(self): return f"A {self.level} school named {self.name} with {self.numberOfStudents} students" class PrimarySchool(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__() return parentRepr + "\n" + "The pickup policy is {pickupPolicy}".format(pickupPolicy = self.pickupPolicy) testSchool = PrimarySchool("Codecademy", 300, "Pickup Allowed") print(testSchool.get_pickupPolicy()) print(testSchool)


I am now getting these errors that I cannot fix. I even double checked the hint given and it is written exactly like it

It seems to be saying that parentRepr is None.
Check that the __repr__ function for the school class is done properly.


Thsi is what I have for the School repr()

That __repr__ function doesn’t return a string, it prints stuff.

And please paste the code after using the </> button instead of using screenshots. Screenshots are hard to read, and text can’t be copied from them.

def repr(School):
print(“A {level} school named {name} with {numberOfStudents} students”)
</>

what I have is written as a print statement not a return statement

A __repr__ function should return a string, not print stuff.