Python TypeErr:Object";int";不可呼叫

Python TypeErr:Object";int";不可呼叫,python,python-3.x,Python,Python 3.x,我试着做一个“打字游戏”,一开始,效果不错。但是当我把代码翻译成英语(变量名、类名、函数名等)时,它给了我一个警告:“objectint是不可调用的”。我怎样才能解决这个问题 这是我的密码: import time import random import sys class Player(): def __init__(self, name, health = 5, energy = 10): self.name = name self.health

我试着做一个“打字游戏”,一开始,效果不错。但是当我把代码翻译成英语(变量名、类名、函数名等)时,它给了我一个警告:“objectint是不可调用的”。我怎样才能解决这个问题

这是我的密码:

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: a\nRun: r\nInformation: i\nExit: 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
导入时间
随机输入
导入系统
类播放器():
定义初始化(自我、姓名、健康=5、能量=10):
self.name=名称
自我健康
自我能量=能量
self.hit=0
def inf(自我):
打印(“健康:”,self.Health,“\n能量:”,self.energy,“\n名称:”,self.name)
def攻击(自身、对手):
打印(“攻击”)
时间。睡眠(.300)
对于范围(3)中的i:
打印(“.”,end=“”,flush=True)
x=self.randomAttack()
如果x==0:
打印(“无人攻击”)
elif x==1:
打印(“{}点击{}!”。格式(名称、对方名称))
自我打击(对手)
对手健康-=1
elif x==2:
打印(“{}点击{}!”。格式(对方名称,名称))
对手。击中(自己)
自我健康-=1
def随机攻击(自我):
返回random.randint(0,2)
def命中(自身,命中):
hit.hitVariable+=1
命中率。能量-=1
如果(hit.hit变量%5)==0:
hit.health-=1
如果hit.health<1:
hit.energy=0
打印(“{}赢得了比赛!”.format(self.name))
self.exit()
@类方法
def出口(cls):
sys.exit()
def运行(自):
打印(“正在运行…”)
时间。睡眠(.300)
打印(“对手抓住你!”)
#######################################################
打印(“欢迎!\n------”)
name=输入(“您叫什么名字?\n>>”)
opponentName=输入(“你的对手叫什么名字?\n>>”)
你=玩家(姓名)
对手=玩家(对手名称)
打印(“命令:\nAttack:a\n运行:r\n信息:i\nExit:e”)
尽管如此:
x=输入(“>>”)
如果x==“a”:
你,攻击(对手)
elif x==“r”:
你跑()
elif x==“i”:
you.inf()
elif x==“e”:
Player.exit()
打破
其他:
打印(“未找到命令!”)
持续

它给出了第24行的错误(self.hit(对手))。

您的
hit
函数就是问题所在。您有相同名称的成员和函数。换一个

还要重命名
hit()
的hit参数。你通过self.hit(对手)来调用它,所以我将它重命名为
def hit(self,对手):

def\uuuuu init\uuuuuuu(自我、姓名、健康=5、能量=10):
self.hit=0
def命中(自身,命中):
hit.hitVariable+=1
命中率。能量-=1
如果(hit.hit变量%5)==0:
hit.health-=1
如果hit.health<1:
hit.energy=0
打印(“{}赢得了比赛!”.format(self.name))
self.exit()

您有一个函数和一个字段,它们都被称为
点击
,请更改其中一个,谢谢。这就解决了问题。
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()