gpt4 book ai didi

python-3.x - 属性错误 : 'function' object has no attribute

转载 作者:行者123 更新时间:2023-12-03 17:37:01 26 4
gpt4 key购买 nike

我一直在尝试制作一款游戏,但是这个错误一直出现。我是 python 的初学者,所以我希望你们甚至可以看看这个可怕的代码。

AttributeError: 'function' object has no attribute 'armorEquipped'

我对这意味着什么感到非常困惑,但有人可以向我解释一下吗?
normalarmor={
"TRAINING ARMOR":["Armor meant for training","no element", +10, " health"]}

firearmor={
"LIGHTBRINGER":["Armor that brings light", "Fire element", +10, " health, Grass type deals less damage"]}

def equipArmor():
print()
m= True
while m==True:
z=True
armorInInventory= len(normalarmor) + len(firearmor) +len(airarmor) +len(grassarmor)+len (waterarmor)
armorInInventory=int(armorInInventory)
print ("You have", armorInInventory, "armors")
print ("You have these armors:")
for name6 in airarmor:
print(name6)
for name2 in normalarmor:
print(name2)
for name3 in firearmor:
print (name3)
for name7 in grassarmor:
print (name7)
for name9 in waterarmor:
print (name9)
print ("Which armor would you like to equip or view")
equipArmor.armorEquipped=input()
equipArmor.armorEquipped= equipArmor.armorEquipped.upper()
if (equipArmor.armorEquipped in normalarmor or
equipArmor.armorEquipped in waterarmor or
equipArmor.armorEquipped in firearmor or
equipArmor.armorEquipped in airarmor or
equipArmor.armorEquipped in grassarmor):
if equipArmor.armorEquipped in normalarmor:
print (normalarmor[equipArmor.armorEquipped])
while z== True:
print ("Equip? Yes or No")
variable1= input()
variable1=variable1.upper()
if variable1== "YES":
print (equipArmor.armorEquipped, "Equipped")
m= False
z= False
elif variable1 == "NO":
z= False

m=True
else:
print ("That is not a valid answer")
z=True
if equipArmor.armorEquipped in firearmor:
print (firearmor[equipArmor.armorEquipped])
while z== True:
print ("Equip? Yes or No")
variable1= input()
variable1 =variable1.upper()
if variable1== "YES":
print (equipArmor.armorEquipped, "Equipped")
m= False
z= False
elif variable1 == "NO":
z= True
else:
print ("That is not a valid answer")
z=True
if equipArmor.armorEquipped in airarmor:
print (airarmor[armorEquipped])
while z== True:
print ("Equip? Yes or No")
variable1= input()
variable1=variable1.upper()
if variable1== "YES":
print (armorEquipped, "Equipped")
z= False
m=False
elif variable1 == "NO":
z= False
m=True
else:
print ("That is not a valid answer")
z=True
if equipArmor.armorEquipped in grassarmor:
print (grassarmor[equipArmor.armorEquipped])
while z== True:
print ("Equip? Yes or No")
variable1= input()
variable1= variable1.upper()
if variable1== "YES":
print (equipArmor.armorEquipped, "Equipped")
x= False
elif variable1 == "NO":
m=True
z= False
else:
print ("That is not a valid answer")
z=True
if equipArmor.armorEquipped in waterarmor:
print (waterarmor[equipArmor.armorEquipped])
while z== True:
print ("Equip? Yes or No")
variable1= input()
variable1= variable1.upper()
if variable1== "YES":
print (equipArmor.armorEquipped, "Equipped")
x= False
elif variable1 == "NO":
m=True
z= False
else:
print ("That is not a valid answer")
z=True

它在这里搞砸了:
def tutorial():
x=True
uhealth= normalarmor[equipArmor.armorEquipped][2]+uhealth

为什么会出现这个问题,这是什么问题?
请帮我 !

最佳答案

首先,让我们切入追逐场景。尽管它可能出现在函数内的函数属性中,但这并不符合人们的预期。但是,可以在外部设置函数的属性。

>>> def f():
... f.a = 1
... return 42
...
>>> f.a
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'function' object has no attribute 'a'
>>> f.b = 2
>>> f.b
2

虽然我不清楚你想完成什么,但可能是 __call__可能会这样做。现在这个对象 class表现得像一个函数,同时,该函数可以设置对象的属性。
>>> class EquipArmour:
... def __call__ (self, param):
... if param == 1:
... self.armourEquipped = 52
... else:
... self.armourEquipped = -34
...
>>> equiparmour = EquipArmour()
>>> result = equiparmour(1)
>>> if equiparmour.armourEquipped == 34:
... 'say hello'
...

关于python-3.x - 属性错误 : 'function' object has no attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com