2017-07-14 737 views
2

我一直在嘗試做一個遊戲,但是這個錯誤不斷出現。我是python的初學者,所以我希望你們甚至可以看看這個可怕的代碼。AttributeError:'函數'對象沒有屬性

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

如何在代碼中定義'equipArmor'? –

+0

哎呀,我忘了分享代碼的主要代碼。對不起! – TheGamerCow

回答

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' 
... 
+0

我不完全明白你的答案。 t確定__call__的作用 – TheGamerCow

+0

'__call__'是對象內Python的特殊名稱之一。在這個代碼中,我寫了'equiparmour(1)',這實際上就像寫'equiparmour(1).__ call __(1)'。但可愛的是'equiparmour'是一個**對象**。與函數不同,它可以有屬性(和其他方法)。這意味着雖然我無法在對象中的函數I ** can **中設置屬性,如'armourEquipped'。但我仍然可以像使用函數一樣使用該對象。我從兩個世界得到了一些東西,這就是你想要的東西。如果這還不清楚,請繼續詢問。 –

0

@TheGamerCow我有一個24" 屏幕,但您的代碼跑了它

我把它換成像這樣:multiline conditions

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): 

在檢查造型

相關問題