2017-08-14 49 views
0

我對python相當陌生,並且遇到許多以前有過的錯誤,但我無法修復以下推薦基於客戶類型的遊戲的代碼'購買歷史。不明白爲什麼會得到「元組索引超出範圍」的錯誤

game_name = ["COD1", "COD2", "COD3", "SKYRIM", "H1Z1", "CSGO", "BF1", "BF3", "BF4", "GTAV", "GTAIV", "FIFA17", "NBA2K17", "MADDEN17", "PES2017", "NHL17", "MLB17", "PGA17", "F12017", "NBA2017"] 
history = [["COD1", "COD2", "SKYRIM", "H1Z1", "NHL17", "MLB17", "CSGO", "BF1", "BF3", "F12017"], ["FIFA17", "NBA2K17", "MADDEN17", "PES2017", "COD3", "SKYRIM", "MLB17", "PGA17", "NBA2017", "GTAIV"], ["COD2", "COD3", "PES2017", "NHL17", "BF4", "GTAV", "GTAIV", "MLB17", "PGA17", "F12017"]] 
forename = ["Joe", "George", "Oliver"] 
HistoryGenre=[["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], ["Sport", "Sport", "Sport", "Sport", "Action", "Action", "Sport", "Sport", "Sport", "Action"], ["Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport", "Sport", "Sport"]] 
rr=1 
ActionCounter=0 
SportCounter=0 
while rr==1: 
    rec=input("Who would you like to recommend games for?") 
    if rec in forename: 
     rr+=1 
     r=forename.index(rec) 
     ActionCounter = HistoryGenre[r][:10].count("Action") 
     SportCounter = HistoryGenre[r][:10].count("Sport") 
     if SportCounter==ActionCounter: 
      both_list=game_name 
      both_remover=0 
      for i in both_list: 
       if both_list[both_remover] in history[r]: 
        both_list.remove(both_list[both_remover]) 
       else: 
        both_remover+=1 
        loops=1 
      for x in range(1): 
       rec1=random.randint(0,len(both_list)-1) 
       while loops==1: 
        for x in range(1): 
         rec2=random.randint(0,len(both_list)-1) 
         if rec2!=rec1: 
          loops+=1 
         else: 
          loops=1 
        loops1=1 
        while loops1==1: 
         for x in range(1): 
          rec3=random.randint(0,len(both_list)-1) 
          if rec3!=rec2 and rec3!=rec1: 
           loops1+=1 
          else: 
           loops1=1 

        print("\n") 
        print("{} likes both genres! I recommend {} and {} and {} and {}".format(rec, both_list[rec1], both_list[rec2], both_list[rec3])) 

錯誤消息發生在代碼的最後一行,並讀取Tuple index out of range,即使both_list不是tuple?其他人似乎有問題,因爲我格式化數組中的索引,但我沒有嘗試過似乎工作,並出現相同的錯誤信息,我不知道我的錯誤的確切來源。

+2

請注意,您的字符串中最後有5個{},但只有四個輸入項 – patrick

+0

我很蠢。複製並粘貼錯誤。 –

回答

1

看起來您在format()調用中的參數比{}的調參數少。

+0

對不起,複製粘貼錯誤:( –

相關問題