更改《Python大学实用教程》代码错误

书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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值