[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Please explain what I've done wro
it seems to me that you should not write Person.population
etc, but self.population
When all person. is replaced by self. the code works as promised!
:
class Person:
population=0
def __init__(self,name):
self.name=name
print '(Initialising %s)' % self.name
self.population += 1
def __del__(self):
print "%s says bye." % self.name
self.population -= 1
if self.population == 0:
print "I am the last one"
else:
print "There are still %d people left." % self.population
def sayHi(self):
'''Greeting by the person.
That's all it does.'''
print "Hi, my name is %s" % self.name
def howMany(self):
if self.population==1:
print "I am on the only person here."
else:
print "We have %d persons here." % self.population
Jason=Person("Jason")
Jason.sayHi()
Jason.howMany()
Sophie=Person("Sophie")
Sophie.sayHi()
Sophie.howMany()
Jason.sayHi()