书p140-p141中,代码如下:
class Superman:
def __init__(self,name):
self.name=name
self.gender=1
self.signal=1
self.illness=False
def nine_negative_kungfu(self):
return 'ya!you have to die!'
zhangsan=Superman('zhangsan')
print("the superman name is:",zhangsan.name)
print("superman is(0-female,1-male)",zhangsan.gender)
result=zhangsan.nine_negative_kungfu()
print("if superman plays nine negative kungfu,the result is:")
print(result)
错误如下:
Traceback (most recent call last):
File "c8.py", line 1, in <module>
class Superman:
File "c8.py", line 9, in Superman
zhangsan=Superman('zhangsan')
PS D:\python1\nine> python c8.py
Traceback (most recent call last):
File "c8.py", line 1, in <module>
class Superman:
zhangsan=Superman('zhangsan')
NameError: name 'Superman' is not defined
更改代码为:
class Superman:[code=python][/code]
def __init__(self,name):
self.name=name
self.gender=1
self.signal=1
self.illness=False
def nine_negative_kungfu(self):
return 'ya!you have to die!'
zhangsan=Superman('zhangsan')
print("the superman name is:",zhangsan.name)
print("superman is(0-female,1-male)",zhangsan.gender)
result=zhangsan.nine_negative_kungfu()
print("if superman plays nine negative kungfu,the result is:")
print(result)
输出:
the superman name is: zhangsan
superman is(0-female,1-male) 1
if superman plays nine negative kungfu,the result is:
ya!you have to die!