将变量值从另一个文件更改为类

时间:2017-11-03 01:48:04

标签: python python-3.x class typeerror

我有一个名为player py的文件,其中包含变量player_weapon = items.WBrokenSworditems.WBrokenSword代表:

class Weapon(Item):
def __init__(self, desc, name, usable, value, damage):
    super().__init__(desc=desc,
                     name=name,
                     usable=usable,
                     value=value)
    self.damage = damage

def __str__(self):
    return "\n{}\n{}\nValue: {}\nDamage: {}\n".format(self.name, self.desc, self.value, self.damage)

class BrokenSword(Weapon):

    def __init__(self):
        super().__init__(name="Espada Danificada",
                         desc="Uma espada pouco resistente.",
                         value=1,
                         usable=0,
                         damage=1)
WBrokenSword = BrokenSword()

combat.py中,我有这个:

player_damage = player.player_weapon.damage
enemy_health -= player_damage

这很好用。在inventory.py中,我有这个:

Inv = [items.WRustySword]
print(Inv)
invaction = input("Which item do you want to use?")
intaction = int(invaction)
player.equip(intaction)

player.equip()表示:

def equip(what)
    import inventory
    player_weapon = inventory.Inv[what]
    return player_weapon  # just for testing

它会返回正确的信息(WRustySword就像WBrokenSword但有不同的数字),但我收到此错误:

TypeError: list indices must be integers or slices, not Rusty Sword

我一直试图解决这个问题2天,我甚至多次更改了代码,在互联网上搜索,没有任何效果。如何让player.player_weapon成为items.WRustySword?我希望我提供了足够的信息,但如果你需要,我总能提供更多信息。

0 个答案:

没有答案
相关问题