4

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 !

ZF007
  • 3,708
  • 8
  • 29
  • 48
ypranite
  • 103
  • 1
  • 1
  • 8

2 Answers2

5

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'
... 
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • I don't exactly get your answer. I'm not really sure what __call__ does – ypranite Jul 14 '17 at 21:10
  • `__call__` is one of Python's special names inside an object. In this code where I wrote `equiparmour(1)` it's practically like writing `equiparmour(1).__call__(1)`. But the lovely thing is that `equiparmour` is an **object**. Unlike a function it can have properties (and other methods). This means that although I couldn't set a property such as `armourEquipped` in a function I **can** in an object. Yet I can still use that object like a function. I get something from both worlds, which is what you wanted in the first place. If this isn't clear yet keep asking. – Bill Bell Jul 14 '17 at 21:31
1

@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

ZF007
  • 3,708
  • 8
  • 29
  • 48