我一直试图制作游戏,但这一个错误不断出现。我是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
为什么会出现此问题,这是什么问题? 请帮帮我!
答案 0 :(得分:4)
首先,让我们切入追逐场景。虽然它可能看起来是函数中函数的一个属性,但它并不能达到预期的效果。但是,函数的属性可以设置在外面。
>>> 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'
...
答案 1 :(得分:0)
@TheGamerCow我有一个24英寸的屏幕,但你的代码已经用完了。
我已经取代它:
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):
检查样式:multiline conditions