2012-10-05 29 views
0

我想爲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$ 

P.S.我使用python 2.7

+4

打印武器 - 錯誤告訴你什麼是你試圖訪問不存在的東西;武器可能只有一個或兩個物品在裏面? – ernie

+0

如果我是你,我會在該行之前打印len(武器)。顯然,名單上的武器不到3個,這意味着當你打電話給武器時[2],那裏沒有任何武器。 – ajon

+0

我想我們得到@lerugray;問題在於,在你的buy_weapons函數中,第一行是「打印武器」,只是爲了調試你的代碼。 ..你可能沒有通過你認爲你正在傳遞的信息 – ernie

回答

1

在您的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'] 
相關問題