I have this simple script but i get an operand error in Line 16
Code:
class Person:
number_of_people = 0
Gravity = -9.5
def __init__(self, name):
self.name = name
Person.add_person()
@classmethod
def number_of_people(cls):
return cls.number_of_people
@classmethod
def add_person(cls):
cls.number_of_people += 1
p1 = Person("joe")
p2 = Person("frank")
print(Person.number_of_peple())
Error message:
Traceback (most recent call last):
File "app4.py", line 19, in <module>
p1 = Person("joe")
File "app4.py", line 7, in __init__
Person.add_person()
File "app4.py", line 16, in add_person
cls.number_of_people += 1
TypeError: unsupported operand type(s) for +=: 'method' and 'int'
What can i do to fix this error?
In this question it say i need to take the varaible name but I only want to increment and not have a variable for that
TypeError: unsupported operand type(s) for +=: 'method' and 'int' (Python)
number_of_people = 0
and methoddef number_of_people(cls):
with the same name. Are you attempting to use a property here?