繁体   English   中英

为什么会出现此索引错误?

[英]Why am I getting this Index Error?

我正在尝试为LPTHW做额外的功课分配,我想我可能正在尝试一些我不知道该功能真正目的的东西。 我正在尝试测试购买的东西,特别是在我的代码中,如果用户购买了某个商品,它将从其当前商品列表中弹出该商品,并将其替换为新商品。 测试购买功能时,出现索引错误。 关于如何使其正常工作的任何想法?

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 "sword" in weapon_choice:
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append(current_weapon)
        elif weapons[1] in weapon_choice:
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append(current_weapon)
        elif weapons[2] in weapon_choice:
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append(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.pop(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append(current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append(current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append(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.pop(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append(current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append(current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append(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.pop(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append(current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append(current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append(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.pop(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append(current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append(current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.pop(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append(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))

# 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']

这是我尝试运行文件的位置。

pp.pprint(character_sheet)
raw_input(t.white_on_red("Please Press Enter To Buy A Weapon"))
buy_weapon(level_zero_weapons)
buy_weapon(level_one_weapons)
pp = pprint.PrettyPrinter(indent=30)
pp.pprint(character_sheet)

最后是我的输出。

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

Traceback (most recent call last):
  File "lodarena.py", line 398, in <module>
    character_gen()
  File "lodarena.py", line 393, in character_gen
    buy_weapon(level_one_weapons)
  File "lodarena.py", line 139, in buy_weapon
    """ % (weapons[0], sword_price, weapons[1], blunt_price, weapons[2], agile_price)
IndexError: list index out of range
Raymond-Weisss-MacBook-Pro:lodarena Raylug$ 

PS我使用Python 2.7

buy_weapons函数中,将第一行设置为“打印武器”,以调试您的代码。 你可能没有通过你以为你在通过的东西

而且,当您学习python时,并且您正在做我们大多数人在第一次学习语言时所做的相同的事情,您可能希望研究一些数据结构。 我猜您还没有准备好对象,但是使用嵌套字典可能值得,例如:

weapons = {1 : {'sword': {'name':'name here',
                            'price': 123},
                  'blunt': {'name':'name here',
                            'price': 123},
                  'agile': {'name':'name here',
                            'price': 123},
                  },
           2 : {'sword': {'name':'name here',
                            'price': 123},
                  'blunt': {'name':'name here',
                            'price': 123},
                  'agile': {'name':'name here',
                            'price': 123},
                  },
          #add levels as you need
          }

基本上,您正在创建一个精美的查找类型的事物,该事物跟踪,级别,然后是武器类型,并带有名称和价格,您可以像这样访问它们:

level = 1
type = 'sword'
weaponname = weapons[level][type]['name']
weaponprice = weapons[level][type]['price']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM