[英]Trying to add values in python but comes up with int not callable error
I am trying to add multiple integers from a class in python however it comes up with the error: 我正在尝试从python中的一个类添加多个整数,但是它带有以下错误:
Traceback (most recent call last): File "G:/documents/Computing/Python/Fighter Game.py", line 53, in if player.health() + player.strength() + player.defence() + player.speed() == 350: TypeError: 'int' object is not callable
追溯(最近一次通话):文件“ G:/ documents / Computing / Python / Fighter Game.py”,第53行,如果player.health()+ player.strength()+ player.defence()+ player。 speed()== 350:TypeError:'int'对象不可调用
The code for the class is: 该类的代码是:
class Fighter:
name = "Not Set"
alignment = "Not Set" #good / bad
health = 0
strength = 0
defence = 0
speed = 0
def name(self):
return self.name
def alignment(self):
return self.alignment
def health(self):
return self.health
def strength(self):
return self.strength
def defence(self):
return self.defence
def speed(self):
return self.speed
def set_name(self, new_name):
self.name = new_name
def set_alignment(self, new_alignment):
self.alignment = new_alignment
def set_health(self, new_health):
self.health = new_health
def set_strength(self, new_strength):
self.strength = new_strength
def set_defence(self, new_defence):
self.defence = new_defence
def set_speed(self, new_speed):
self.speed = new_speed
and the code that throws up the error is: 引发错误的代码是:
player = Fighter()
while True:
player.set_name(input("Enter your name: "))
player.set_alignment("good")
player.set_health(int(input("Enter your health: ")))
player.set_strength(int(input("Enter your strength: ")))
player.set_defence(int(input("Enter your defence: ")))
player.set_speed(int(input("Enter your speed: ")))
if player.health() + player.strength() + player.defence() + player.speed() == 350:
print("Player setup complete.")
break
else:
print("Numerical player values must all add up to 350.")
Any help that could be given would be appreciated! 任何可以提供的帮助将不胜感激! :-)
:-)
You have two things that are called self.health
; 你有两件事情被称为
self.health
; an integer and a method. 整数和方法。 This doesn't make sense.
这没有道理。
Instance variables are often prefixed with _
, and set up in __init__()
, at least this is the "old-school" simple way of doing it: 实例变量通常以
_
为前缀,并在__init__()
,至少这是“老式”的简单做法:
def __init__(self):
self._health = 40
def health(self):
return self._health
And so on. 等等。 But do look into attributes and properties for more elegant ways of doing this.
但是请仔细研究属性和属性,以获取更优雅的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.