我的 RPG 相关问题已经变形。我有一个 dict 武器块,用(我相信)它们的价值和伤害来定义。我如何生成某种武器作为输出,例如,供商人出售?
这是武器类:
class weapon(object):
def __init__(name, loot_worth, damage):
Character.__init__(self)
self.damage = Damage
def Damage(weapon):
self.damage = Damage
dict块的段:
weapon_dict = {"None" : [0, 0],
"Imaginary Sword" : [0, 0],
"Twig" : [0, 1]
}
merchHasWeapon 功能块:
def merchHasWeapon(self):
if self.merchstate == 'buy':
return random.choice(weapon_dict.keys())
还有商品功能:
def merch(self):
self.merchstate == 'buy'
self.merchstim = randint (0, 10)
self.merchammo = randint (0, 50)
if randint(0, 1):
temp_wpn = self.merchHasWeapon()
temo_armr = self.merchHasArmor()
print("Merch weapon: {} , Stats: {}".format(temp_wpn,weapon_dict[temp_wpn]))
print("Merch armor: {} , Stats: {}".format(temo_armr,armor_dict[temo_armr]))
print "%s goes to the Merchants' Clearing. For help with merchants, type mh." % self.name
print "Merchant's items:\n~Potions:%d\n~Arrows:%d\n" % (self.merchstim, self.merchammo)
如果“def merchHasWeapon”块出现在“def merch”块之前,则打印的错误消息是“格式中的零长度字段名称”。如果之后出现,则显示“未定义全局名称商品”。有人可以帮我纠正这个错误吗?