script not fancy at all, just for educational purposes but i want it to work properly and learn how to fix this problem...
it's an easy problem the errormessage says
NameError: name 'person' is not defined
but from what i know it has been defined, it's my class
here's the 3 first lines in my code....
class person:
'''represents a person'''
person.population = 0
...where i get this error.
when i ran it for the first time it was fine untill line 12 and 39 and there i got this error
Traceback (most recent call last):
File "C:/class_object_variables.py", line 39, in <module>
alex = person('Alex')
and this
File "C:/Users/class_object_variables.py", line 12, in __init__
population += 1
UnboundLocalError: local variable 'population' referenced before assignment
no idea what the line39 error is all about but the one on line 12 i may have a clue about, just don't know how i'll get it to work properly.... maybe i have to put person.population to get it work?
annoying idd...
i like this community very much nice and very very helpful people from all around the world connected with eachother, thats why i came here and not just googled it and i hope to get good useful answers.
try not to YELL out the answer directly, appriciated.
here comes the rest
class person:
'''represents a person'''
person.population = 0
def __init__(self, name):
'''initialize the persons data.'''
self.name = name
print '(Initializing %s)' % self.name
#when this person is created, he/she
#adds to the population
person.population += 1
def __del__(self):
'''i am dying'''
print '%s says bye...' % self.name
personpopulation -= 1
if person.population == 0:
print 'i am the last one.'
else:
print 'there are still %d people left.' % person.populatio
def sayHi(self):
'''Greeting by the person'''
print 'Hi! My name is %s' % self.name
def howMany(self):
'''prints the current population'''
if person.population == 1:
print 'i am the only person here.'
else:
print 'we have %d persons here.' % person.population
alex = person('Alex')
alex.sayHi()
alex.howMany()
kalam = person('lisa')
kalam.sayHi()
kalam.howMany()
alex.sayHi()
alex.howMany()
i'm very new to python just so you know
have a nice day folks!
peace

New Topic/Question
Reply


MultiQuote



|