所以我得到一個「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
提示:我敢打賭,info ['rnum']'大於'len(con_owned)'。 – Kevin 2014-10-01 12:59:14
info ['rnum']的值是什麼 – 2014-10-01 13:02:00
使用info ['rnum'] = raw_input的範圍在1到5之間 – Surf3rDud3 2014-10-01 13:08:54