我正在尝试制作泥浆,但是在发出装备命令时遇到了这个错误。
if item.types == "weapon":
AttributeError: 'unicode' object has no attribute 'types'
我想修复我的代码,以便它允许用户输入装备剑,它会在他/她的库存中搜索该项目并应用统计信息。
这是我的商品代码
#weapons
class Weapon():
def __init__(self, name, ATK, gold, types):
self.name = name
self.ATK = ATK
self.gold = gold
self.types = types
club = Weapon("club", 1, 3, "weapon")
sword = Weapon("sword", 3, 10,"weapon")
axe = Weapon("axe", 4, 15,"weapon")
bow = Weapon("Hailey's bow", 10, 40,"weapon")
swordX = Weapon("Gods Sword", 8000, 8000,"weapon")
这是我的播放器代码
players[id] = {
"name": None,
"room": "Tavern",
"ATK": 5,
"hp": 20,
"inventory": {},
"armorName": None,
"armorPT": None,
"weaponName": None,
"weaponPT": None,
}
最后我的装备命令代码
elif command == "equip":
x = params.lower()
rm = rooms[players[id]["room"]]
if x in players[id]["inventory"]:
item = players[id]["inventory"][x]
weapon = players[id]["weaponName"]
wStat = players[id]["weaponPT"]
armor = players[id]["armorName"]
aStat = players[id]["armorPT"]
hp = players[id]["hp"]
ATK = players[id]["ATK"]
if item.types == "weapon":
weapon = item.name
wStat = item.ATK
ATK += wStat
mud.send_message(id,"you equip %s" % weapon)
mud.send_message(id,"NEW ATK: %d" % ATK)
elif item.type == "armor":
armor = item.name
aStat = item.DEF
hp += aStat
mud.send_message(id,"you equip %s" % armor)
mud.send_message(id,"NEW DEF: %d" % hp)
else:
mud.send_message(id,"not a vaid item type")
else:
mud.send_message(id,"you dont have this item")
如果您认为我需要添加更多代码才能使其有意义,请告诉我。
编辑:我想我找到了问题所在,但我不知道如何解决它我相信它在 take 命令中,它被添加到库存中,它被添加为我不知道如何添加的字符串解决这个问题
这是代码
elif command == "take":
x = params.lower()
rm = rooms[players[id]["room"]]
if rm["item"] == "yes":
if x in rm["itemName"]:
players[id]["inventory"][x] = x
del rm["itemName"][x]
mud.send_message(id, "you picked up %s" % x)
print players[id]["inventory"][x]
else:
mud.send_message(id,"You dont see that item")
else:
mud.send_message(id,"there is no item here")