数组.append(str(con_owned[i])索引器:列表索引超出范围

2024-04-25 23:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我得到了一个“数组.append(str(con\u owned[i])索引器:列表索引超出范围“在我的终端中,我有点不知所措,有什么帮助吗?你知道吗

下面是请求的其余代码。我输入一个介于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

Tags: theininfoyoucityinputrawcalc
2条回答

请你提供一下信息字典好吗?因为没有它,我们就无法在我们这边复制你的问题,这就更难帮助你了。你知道吗

尽管我敢打赌你的info['rnum']比你的控制台列表要大,因为你的程序似乎试图打开一个不存在的列表索引。(例如,con\u owned[9],因为con\u owned有索引0到8的值)

编辑: 只需提供整个程序,因为我刚刚尝试了您发布的5 for info['rnum']的内容,它毫无问题地工作了。你知道吗

编辑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。你知道吗

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

试试这个:

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

因为我认为在info['rnum']中,不是得到int,而是得到str

我希望这有帮助

相关问题 更多 >