|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码运行第一次可以成功,但是第二次后运行出错。
Traceback (most recent call last):
File "C:/Users/86166/Desktop/a.py", line 24, in <module>
player.hp = player.hp - enemy.attack()
TypeError: 'int' object is not callable
- import random
- class Role():
- def __init__(self,hp):
- self.hp=hp
- def attack(self):
- self.attack=random.randint(1,10)
- return self.attack
- def defense(self):
- self.defense=0.5*self.attack()
- return self.defense
- def hp_result(self):
- pass
- player=Role(100)
- enemy=Role(100)
- while player.hp>0 or enemy.hp>0:
- print("player's hp = {}".format(player.hp))
- print("enemy's hp = {}".format(enemy.hp))
- this_result = False
- while not this_result:
- result = input("请选择攻击(A)还是防守(D)")
- if result == 'A' :
- this_result = True
- player.hp = player.hp - enemy.attack()
- enemy.hp = enemy.hp - player.attack()
- elif result == 'D':
- this_result = True
- player.hp = player.hp - enemy.defense()
- else:
- print("请输入正确的字母")
复制代码
属性名覆盖了方法名,将属性名改一下就好了。
代码帮你改好了:
- import random
- class Role():
- def __init__(self, hp):
- self.hp = hp
- def attack(self):
- self.a = random.randint(1, 10)
- return self.a
- def defense(self):
- self.d = 0.5 * self.attack()
- return self.d
- def hp_result(self):
- pass
- player = Role(100)
- enemy = Role(100)
- while player.hp > 0 or enemy.hp > 0:
- print("player's hp = {}".format(player.hp))
- print("enemy's hp = {}".format(enemy.hp))
- this_result = False
- while not this_result:
- result = input("请选择攻击(A)还是防守(D)")
- if result == 'A':
- this_result = True
- player.hp = player.hp - enemy.attack()
- enemy.hp = enemy.hp - player.attack()
- elif result == 'D':
- this_result = True
- player.hp = player.hp - enemy.defense()
- else:
- print("请输入正确的字母")
复制代码
|
|