AttributeError: 'function' object has no attribute
我一直在尝试做一个游戏,但这一个错误不断出现。我是Python的初学者,所以我希望你们能看看这个可怕的代码。
1 | AttributeError: 'function' object has no attribute 'armorEquipped' |
我很困惑这意味着什么,但有人能给我解释一下吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 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 |
它把这里搞得一团糟:
1 2 3 | def tutorial(): x=True uhealth= normalarmor[equipArmor.armorEquipped][2]+uhealth |
为什么会出现这个问题?这个问题是什么?请帮帮我!
首先,让我们转到追逐的场景。虽然它可能在函数中显示一个函数的属性,但这并不能满足人们的期望。但是,可以在外部设置函数的属性。
1 2 3 4 5 6 7 8 9 10 11 | >>> 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 |
虽然我不清楚你想要完成什么,但可能是
1 2 3 4 5 6 7 8 9 10 11 12 | >>> 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' ... |
@GamerCow我有一个24英寸的屏幕,但你的代码用完了。
我已经这样替换了它:
1 2 3 4 5 | 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): |
在:多行条件下检查样式