array.append(str(con_owned [i]))IndexError:列表索引超出范围

时间:2014-10-01 12:58:03

标签: python

所以我得到一个“array.append(str(con_owned [i]))IndexError:list index out of range”在我的终端中,我有点无能为力?

以下是请求的其余代码。我输入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

2 个答案:

答案 0 :(得分:0)

请您提供信息词典吗?因为没有它我们就无法在我们的问题上复制你的问题,这使你更难以帮助你。

虽然我敢打赌你的info['rnum']大于你的控制台列表,因为你的程序似乎试图打开一个不存在的列表索引。(即con_owned [9]因为con_owned有值索引0到8)

编辑: 只需提供整个程序,因为我刚刚尝试使用5 info['rnum']发布的内容并且它没有问题。

编辑2: 这是解决方案,替换:

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

with:

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

你不小心在该函数中使用了rnum而不是rnum2。

另外,将所有应该是数字的raw_input放在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): '))

答案 1 :(得分:0)

试试这个:

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

因为我认为在info['rnum']中,没有得到一个int,并且正在获得一个str。

我希望这会有所帮助