1

我的 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”块之前,则打印的错误消息是“格式中的零长度字段名称”。如果之后出现,则显示“未定义全局名称商品”。有人可以帮我纠正这个错误吗?

4

1 回答 1

1

问题是这样的:

if randint(0, 1):
    print("Merch weapon: {} , Stats: {}".format(merch_weapon,weapon_dict[merch_weapon]))
    print("Merch armor: {} , Stats: {}".format(merch_armor, armor_dict[merch_armor]))

首先,merch_weapon是一个函数,所以你实际上必须通过 do 来调用它self.merch_weapon()。接下来,您的merch_weapon函数应该返回一些内容,以便您可以在访问字典时使用它:

def merch_weapon(self):
    if self.merchstate == 'buy':
      return random.choice(weapon_dict.keys()) # list() isn't needed here

现在,当你打印你的武器和盔甲数据时,不要忘记括号:

if randint(0, 1):
    temp_wpn = merch_weapon()
    temo_armr = merch_armor()
    print("Merch weapon: {} , Stats: {}".format(temp_wpn, weapon_dict[temp_wpn]))
    print("Merch armor: {} , Stats: {}".format(temo_armr, armor_dict[temo_armr]))
于 2013-06-30T02:49:25.613 回答