简体   繁体   中英

AttributeError: 'function' object has no attribute

I've been trying to make a game, but this one error keeps appearing. I'm a beginner in python so I hope you guys can even look at this horrendous code.

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

I'm very confused on what this means but could someone explain it to me?

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

and it messes up around here:

def tutorial():
    x=True
    uhealth= normalarmor[equipArmor.armorEquipped][2]+uhealth 

Why is this problem appearing and what is this problem? Please help me !

First of all, let's cut to the chase scene. Although it might appear an attribute of a function within the function this doesn't do what one might expect. The function's attributes can , however, be set outside.

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

Although I'm not clear about what you want to accomplish it might be that __call__ might do it. Now an object of this class behaves like a function and, at the same time, the function can set attributes of the object.

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

@TheGamerCow I've got a 24" screen but your code ran off it.

I've replaced it like this:

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

Check for styling at: multiline conditions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM