2014-10-01 37 views
0

所以我得到一個「array.append(str(con_owned [i]))IndexError:list index out of range」in我的終端,我有點無知任何幫助?array.append(str(con_owned [i]))IndexError:列表索引超出範圍

這是請求的其餘代碼。我輸入1到5之間的數字,但仍然出現錯誤。

info = dict() 

info['console'] = raw_input('The game console you own? ') 
info['city'] = raw_input('The city you live in? ') 
info['wconsole'] = raw_input('The console you would like? ') 
info['rnum'] = raw_input('The number of consoles you own between 1 & 5? ') 
info['rnum2'] = raw_input('A number between 1 and 12: ') 
info['wcity'] = raw_input('Enter a number from 1 to 7: ') 
info['float'] = float(input('Amount of money made per week (ex. 1.23): ')) 

if info['rnum'] >5: 
    info['rnum'] = 5 
elif info['rnum'] <1: 
    info['rnum'] = 1 

if info['rnum2'] >12: 
    info['rnum'] = 12 
elif info['rnum2'] <1: 
    info['rnum2'] = 1 

if info['wcity'] >7: 
    info['wcity'] = 7 
elif info['wcity'] <1: 
    info['wcity'] = 1 

con_owned = ['Xbox 360', 'Xbox One', 'Playstation 3', 'Playstation 4', 'Wii', 'WiiU', 'Xbox', 'Playstation 2', 'Gamecube'] 
con_owned.insert(0,str(info['console'])) 

array = [] 

for i in range(info['rnum']): 
    array.append(str(con_owned[i])) 

console_list = "" 

for console in array: 
    console_list = console_list + console + ", " 

def calc_price(): 
    decimal = info['float'] 
    dollar_amount = decimal * 10 
    return dollar_amount 

calc_price = calc_price() 

wcities =['Paris', 'Hollywood', 'London', 'Hong Kong', 'Dublin', 'Orlando', 'Dallas'] 
wcity = wcities[(info['wcity']-1)] 

message = '''I have a {info[console]} that I play at my house in the city of {info[city]}. 
I currently own {into[rnum]} consoles. I really want to have a {info[wconsole]} and play it in {wcity}. One day I would like own {info[rnum2]} consoles. 
I wish I could make ${calc_price} a week, I could afford a {info[dream]} and move to {live}.''' 

messageFormatted = message.format(**locals()) 
print messageFormatted 
+1

提示:我敢打賭,info ['rnum']'大於'len(con_owned)'。 – Kevin 2014-10-01 12:59:14

+0

info ['rnum']的值是什麼 – 2014-10-01 13:02:00

+0

使用info ['rnum'] = raw_input的範圍在1到5之間 – Surf3rDud3 2014-10-01 13:08:54

回答

0

您是否介意提供信息字典?因爲如果沒有它,我們就無法在您的問題上覆制您的問題,這使您很難幫助您。

雖然我敢打賭你的info['rnum']大於你的控制檯列表,因爲你的程序似乎試圖打開一個不存在的列表索引(即con_owned [9],因爲con_owned的索引值爲0通過8)

編輯: 只提供整個程序,因爲我剛剛嘗試了你發佈的info['rnum'] 5,它的工作沒有問題。

編輯2: 這裏是解決方案,替換:

if info['rnum2'] >12: 
    info['rnum'] = 12 
elif info['rnum2'] <1: 
    info['rnum2'] = 1 

有:

if info['rnum2'] >12: 
    info['rnum2'] = 12 
elif info['rnum2'] <1: 
    info['rnum2'] = 1 

你不小心使用RNUM代替rnum2該功能。

而且,把你所有的raw_inputs應該是數字在int()

這裏:

info['console'] = raw_input('The game console you own? ') 
info['city'] = raw_input('The city you live in? ') 
info['wconsole'] = int(raw_input('The console you would like? ')) 
info['rnum'] = int(raw_input('The number of consoles you own between 1 & 5? ')) 
info['rnum2'] = int(raw_input('A number between 1 and 12: ')) 
info['wcity'] = int(raw_input('Enter a number from 1 to 7: ')) 
info['float'] = float(input('Amount of money made per week (ex. 1.23): ')) 
+0

添加字典信息 – Surf3rDud3 2014-10-01 13:14:08

+0

Hm ...你有沒有想過將raw_input的結果轉換爲一個整數? 沒有,會拋出另一個錯誤... – inirlan 2014-10-01 13:16:34

+0

我會發布所有的代碼,但它要我寫moron的問題,我真的不知道還有什麼要說。 :/ – Surf3rDud3 2014-10-01 13:18:36

0

試試這個:

info['rnum'] = int(raw_input('The number of consoles you own between 1 & 5? ')) 

因爲我認爲,在info['rnum'],是沒有得到一個int,並獲得一個str。

我希望這可以幫助

+0

con_owned有9個項目,而rnum只能按照指示在1和5之間。 – Surf3rDud3 2014-10-01 13:16:47

+0

但是1和5之間並不是強制性的......如果你輸入100,它會允許,然後你會得到這個問題;)你沒有驗證數字 – 2014-10-01 13:19:51

+0

是的,但我輸入的數字在1和5之間,仍然得到錯誤。 – Surf3rDud3 2014-10-01 13:20:30

相關問題