AttributeError: 'function'对象没有属性

时间:2022-03-14 17:10:35

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.

我一直在尝试做一个游戏,但是这个错误不断出现。我是python的初学者,所以我希望你们能看看这个可怕的代码。

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 !

为什么会出现这个问题,这个问题是什么?请帮助我!

2 个解决方案

#1


4  

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.

虽然我不清楚您想要完成什么,但是__call__可能会完成它。现在这个类的对象的行为就像一个函数,同时,函数可以设置对象的属性。

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

#2


0  

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

@TheGamerCow我有一个24英寸的屏幕,但是你的代码跑掉了。

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

检查样式在:多行条件

#1


4  

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.

虽然我不清楚您想要完成什么,但是__call__可能会完成它。现在这个类的对象的行为就像一个函数,同时,函数可以设置对象的属性。

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

#2


0  

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

@TheGamerCow我有一个24英寸的屏幕,但是你的代码跑掉了。

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

检查样式在:多行条件