鱼C论坛

 找回密码
 立即注册
查看: 2490|回复: 1

[已解决]攻防小游戏代码出错。

[复制链接]
发表于 2020-2-18 14:57:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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





  1. import random
  2. class Role():
  3.     def __init__(self,hp):
  4.         self.hp=hp
  5.     def attack(self):
  6.         self.attack=random.randint(1,10)
  7.         return self.attack
  8.     def defense(self):
  9.         self.defense=0.5*self.attack()
  10.         return self.defense
  11.     def hp_result(self):
  12.         pass
  13. player=Role(100)
  14. enemy=Role(100)

  15. while player.hp>0 or enemy.hp>0:
  16.     print("player's hp = {}".format(player.hp))
  17.     print("enemy's hp = {}".format(enemy.hp))
  18.     this_result = False
  19.     while  not this_result:
  20.         result = input("请选择攻击(A)还是防守(D)")
  21.         if  result == 'A' :
  22.             this_result = True
  23.             player.hp = player.hp - enemy.attack()
  24.             enemy.hp = enemy.hp - player.attack()
  25.         elif  result == 'D':
  26.             this_result = True
  27.             player.hp = player.hp - enemy.defense()
  28.         else:
  29.             print("请输入正确的字母")
复制代码
最佳答案
2020-2-18 14:59:35
属性名覆盖了方法名,将属性名改一下就好了。

代码帮你改好了:

  1. import random


  2. class Role():
  3.     def __init__(self, hp):
  4.         self.hp = hp

  5.     def attack(self):
  6.         self.a = random.randint(1, 10)
  7.         return self.a

  8.     def defense(self):
  9.         self.d = 0.5 * self.attack()
  10.         return self.d

  11.     def hp_result(self):
  12.         pass


  13. player = Role(100)
  14. enemy = Role(100)

  15. while player.hp > 0 or enemy.hp > 0:
  16.     print("player's hp = {}".format(player.hp))
  17.     print("enemy's hp = {}".format(enemy.hp))
  18.     this_result = False
  19.     while not this_result:
  20.         result = input("请选择攻击(A)还是防守(D)")
  21.         if result == 'A':
  22.             this_result = True
  23.             player.hp = player.hp - enemy.attack()
  24.             enemy.hp = enemy.hp - player.attack()
  25.         elif result == 'D':
  26.             this_result = True
  27.             player.hp = player.hp - enemy.defense()
  28.         else:
  29.             print("请输入正确的字母")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-18 14:59:35 | 显示全部楼层    本楼为最佳答案   
属性名覆盖了方法名,将属性名改一下就好了。

代码帮你改好了:

  1. import random


  2. class Role():
  3.     def __init__(self, hp):
  4.         self.hp = hp

  5.     def attack(self):
  6.         self.a = random.randint(1, 10)
  7.         return self.a

  8.     def defense(self):
  9.         self.d = 0.5 * self.attack()
  10.         return self.d

  11.     def hp_result(self):
  12.         pass


  13. player = Role(100)
  14. enemy = Role(100)

  15. while player.hp > 0 or enemy.hp > 0:
  16.     print("player's hp = {}".format(player.hp))
  17.     print("enemy's hp = {}".format(enemy.hp))
  18.     this_result = False
  19.     while not this_result:
  20.         result = input("请选择攻击(A)还是防守(D)")
  21.         if result == 'A':
  22.             this_result = True
  23.             player.hp = player.hp - enemy.attack()
  24.             enemy.hp = enemy.hp - player.attack()
  25.         elif result == 'D':
  26.             this_result = True
  27.             player.hp = player.hp - enemy.defense()
  28.         else:
  29.             print("请输入正确的字母")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 20:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表