0

私はLPTHWのためにこの追加のクレジットを行使しようとしましたが、壁にぶつかりました。値エラーが発生し続け、その理由がわかりません。辞書とクラスを使用する必要があることはわかっていますが、本のその部分にはまだ到達していません。

アイデアは、このインベントリ関数を取得して、以前の武器をリストから削除しcharacter_sheet、ユーザーが購入したばかりの武器に置き換えたいというものです。これが関連するコードであると私が信じているものです。何か不足している場合はお知らせください。

前もって感謝します。スタック オーバーフローは、以前の経験から Python を学ぶことに取り組んでいるので、本当に役に立ちました。

weapon_choice = raw_input(":> ")
        if "sword" in weapon_choice:
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_zero_weapons)

関連するコードの次のビットを次に示します。

weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)

これが私が扱っているリストです。

# Weapon lists 
level_zero_weapons = ['short sword', 'club', 'dagger']
level_one_weapons = ['sword', 'mace', 'rapier']
level_two_weapons = ['long sword', 'morningstar', 'trident']
level_three_weapons = ['claymore', 'flail', 'sycthe']
level_four_weapons = ['bastard sword', 'dragon bone', 'crystal halbred']

そして、これが私の出力です。私はそれを理解していません。さらにコードを追加する必要があるかどうかを教えてください。

Please tell me your name brave soul. :> Ray

        Lets now randomly generate brave gladiator Ray.
[                             'Name: Ray:',
                              'Gender: Male',
                              'Character Class: Warrior',
                              'Strength: 11',
                              'Dexterity: 7',
                              'Constitution: 8',
                              'Damage 1D6',
                              'Crit Chance 10%',
                              'Hit Points: 6/6']
Please Press Enter To Buy A Weapon

Please type in the weapon you want to buy.

short sword, price: 1 gold pieces

club, price: 1 gold pieces

dagger, price: 1 gold pieces.

:> dagger

Your current weapon is now a dagger. Press Enter To Continue


Type in the weapon you want to buy, type quit to return to the barracks.

sword, price: 3 gold pieces

mace, price: 4 gold pieces

rapier, price: 5 gold pieces.

:> sword
Traceback (most recent call last):
  File "lodarena.py", line 399, in <module>
    character_gen()
  File "lodarena.py", line 394, in character_gen
    buy_weapon(level_one_weapons)
  File "lodarena.py", line 144, in buy_weapon
    character_sheet.remove(current_weapon)
ValueError: list.remove(x): x not in list
Raymond-Weisss-MacBook-Pro:lodarena Raylug$ 

編集:これが私の武器購入方法全体です。

# Doing Stuff for weapons and the shopkeeper. #################################

def level_zero_price():
    """Generates the price for level one weapons"""
    return randint(1, 3)

def level_one_price():
    """Generates the price for level two weapons"""
    return randint(3, 6)

def level_two_price():
    """Generates the price for level three weapons"""
    return randint(6, 9)

def level_three_price():
    """Generates the price for level four weapons"""
    return randint(9, 12)

def level_four_price():
    "Generates the price for level four weapons"""
    return randint(12, 15)
### Major Buying Stuff / Inventory Code ##########################################

def buy_weapon(weapons):
    """big bit of code that allows you to buy a weapons from a weapon list.
The function acts a little differently after level zero weapons"""
    global current_weapon
    if weapons == level_zero_weapons:
        sword_price = level_zero_price()
        blunt_price = level_zero_price()
        agile_price = level_zero_price()
        print t.bright_yellow_on_magenta + """
Please type in the weapon you want to buy.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_zero_weapons)

    elif weapons == level_one_weapons:
        sword_price = level_one_price()
        blunt_price = level_one_price()
        agile_price = level_one_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price, weapons[2],
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_one_weapons)

    elif weapons == level_two_weapons:
        sword_price = level_two_price()
        blunt_price = level_two_price()
        agile_price = level_two_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_two_weapons)

    elif weapons == level_three_weapons:
        sword_price = level_three_price()
        blunt_price = level_three_price()
        agile_price = level_three_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_three_weapons)

    elif weapons == level_four_weapons:
        sword_price = level_four_price()
        blunt_price = level_four_price()
        agile_price = level_four_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_four_weapons)      
    else:
        print"~~~There is a bug somwhere, forgot to assign (weapons)\n\n\n"
    raw_input(t.white_on_red("""
Your current weapon is now a %s. Press Enter To Continue
""" % current_weapon))
4

1 に答える 1

3

これ以上のコードを見ないと、確かなことは言えませんが、問題は、武器を追加するときに行っているcharacter_sheet.append("Current Weapon %s" % current_weapon)ことですが、後でしようとしているようですcharacter_sheet.remove(current_weapon)

そこでの最初の操作は、「Current Weapon dagger」のような文字列をリストに追加します。2 番目の操作では、文字列「dagger」をリストから削除しようとします。しかし、正確な文字列 "dagger" はリストにありません。"Current Weapon dagger" はリストにあります。そのため、リストから「短剣」を削除するように指示されたため、Python がクラッシュしていますが、それが見つかりません。一致しない「現在の武器の短剣」しか見つかりません。

この問題を解決するには多くの方法があります。おそらく、Python を難しい方法で学習するのセクションを順番に熱心に取り組んでいて、その方法を学んでいないためです。コードを関数に一般化し、Don't Repeat Yourself の原則 (if/elif/elif ブロックはすべて本質的に同じことを行っているため、「自分自身を繰り返す」ことになります) と適切なデータ構造にデータを格納する方法について説明します。

レッスンを続行してこのプロジェクトを脇に置き、後でまだ興味がある場合は、頻繁に参照して、学習した新しい内容をどのように適用できるかを確認することをお勧めします. 数回のレッスンごとに、このコードを簡素化できる新しいことを学びます。たとえば、武器に文字列の代わりにオブジェクトを使用するなどです。出力を保存するときではなく、表示するときにのみ人間が読めるテキストに出力をフォーマットします。文字シートをリストではなくオブジェクトまたは辞書に変換します。if/elif/elif ブロックを比較のみに使用し、キャラクター シートから武器を関数に追加および削除するビジネス ロジックを一般化します。

この問題を修正したいだけの場合は、削除する文字列をリスト内の文字列と何らかの方法で一致させる必要があります。(長い文字列の代わりに)別のものをリストに入れるかcurrent_weapon、実際に長い文字列自体を検索します。または、それがデータになることが確実な場合は、リストの最後の項目を削除することもできますcurrent_weapon

于 2012-10-06T06:31:39.063 に答える