私の RPG 関連の問題は変形しました。私は武器の辞書ブロックを持っており、それらの価値とダメージで定義されています(私は信じています)。たとえば商人が販売するために、出力として特定の武器を生成するにはどうすればよいですか?
武器クラスは次のとおりです。
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」ブロックの前にある場合に表示されるエラー メッセージは、「フォーマット内のフィールド名の長さがゼロです」です。後に来る場合は、「グローバル ネーム マーチは定義されていません」と表示されます。誰かがこのエラーを修正するのを手伝ってくれますか?