简体   繁体   中英

Why am I getting this Index Error?

I am trying to do an extra credit assignment for LPTHW and I think I am probably trying stuff where I dont know the true purpose of the function. I am trying to test buying things, specifically in my code, if the user buys an item, it pops off the item from their current item list, and replaces it with the new one. When I tested the buying functions, I got an index error. any ideas on how to make this work correctly?

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

Here is where I try to run the file.

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)

and finally here is my output.

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 I m using python 2.7

In your buy_weapons function, make the first line a "print weapons" just to debug your code . . . you're probably not passing what you think you are passing

And while you're learning python, and you're doing the same thing most of us did while first learning a language, you may want to look into some data structures. I'm guessing you're not ready for objects yet, but it might be worth using a nested dictionary, eg:

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
          }

Basically, you're creating a fancy lookup type thing, that tracks, level, then weapon type, with name and price, and you could access them like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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