TypeErr:对象"int"不可调用



我试图制作一个"打字游戏";一开始,结果还不错。但当我把代码翻译成英文(变量名、类名、函数名等(时,它给了我警告";对象int不可调用;。我该如何解决这个问题?

这是我的代码:

import time
import random
import sys
class Player():
def __init__(self, name, health = 5, energy = 10):
self.name = name
self.health = health
self.energy = energy
self.hit = 0
def inf(self):
print("Health: ", self.health, "nEnergy: ", self.energy, "nName: ", self.name)
def attack(self, opponent):
print("Attacking")
time.sleep(.300)
for i in range(3):
print(".", end=" ", flush=True)
x = self.randomAttack()
if x == 0:
print("Nobody attacks.")
elif x == 1:
print("{} hits {} !".format(name, opponentName))
self.hit(opponent)
opponent.health -= 1
elif x == 2:
print("{} hits {}!".format(opponentName, name))
opponent.hit(self)
self.health -= 1
def randomAttack(self):
return random.randint(0, 2)
def hit(self, hit):
hit.hitVariable += 1
hit.energy -= 1
if (hit.hitVariable % 5) == 0:
hit.health -= 1
if  hit.health < 1:
hit.energy = 0
print('{} won the game!'.format(self.name))
self.exit()
@classmethod
def exit(cls):
sys.exit()
def run(self):
print("Running...")
time.sleep(.300)
print("Opponent catch you!")
#######################################################
print("Welcome!n----------")
name = input("What's your name?n>>>")
opponentName = input("What's your opponent's name?n>>>")
you = Player(name)
opponent = Player(opponentName)
print("Commands: nAttack: anRun: rnInformation: inExit: e")
while True:
x = input(">>>")
if x == "a":
you.attack(opponent)
elif x == "r":
you.run()
elif x == "i":
you.inf()
elif x == "e":
Player.exit()
break
else:
print("Command not found!")
continue

它给了我第24行的错误(自身命中(对手((。

问题出在hit函数上。您有一个具有相同名称的成员和一个函数。换一个。

同时重命名hit()的hit参数。你通过self.hit(对手(来称呼它,所以我会把它重命名为def hit(self, opponent):

def __init__(self, name, health = 5, energy = 10):
self.hit = 0

def hit(self, hit):
hit.hitVariable += 1
hit.energy -= 1
if (hit.hitVariable % 5) == 0:
hit.health -= 1
if  hit.health < 1:
hit.energy = 0
print('{} won the game!'.format(self.name))
self.exit()

相关内容

  • 没有找到相关文章

最新更新