Python array.append(str(con_owned[i])索引器:列表索引超出范围
所以我得到了一个数组。appendstrcon_拥有的[I]索引器:列表索引超出了我终端的范围,我有点不知所措。有什么帮助吗 下面是请求的其余代码。我输入了一个介于1和5之间的数字,但仍然会得到错误Python array.append(str(con_owned[i])索引器:列表索引超出范围,python,Python,所以我得到了一个数组。appendstrcon_拥有的[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_inp
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
请你提供一下信息字典好吗?因为没有它,我们就无法在我们这边复制你的问题,这使我们更难帮助你 虽然我打赌你的信息['rnum']比你的控制台列表大,因为你的程序似乎试图打开一个不存在的列表索引。例如,con_owned[9],因为con_owned的索引值为0到8 编辑: 只需提供整个程序,因为我刚刚尝试了你发布的5 for info['rnum']的内容,它工作正常 编辑2: 以下是解决方案,请替换:
if info['rnum2'] >12:
info['rnum'] = 12
elif info['rnum2'] <1:
info['rnum2'] = 1
试试这个:
info['rnum'] = int(raw_input('The number of consoles you own between 1 & 5? '))
因为我认为在info['rnum']中,不是得到int,而是得到str
我希望这有帮助提示:我打赌info['rnum']比lencon_拥有的要大。info['rnum']的价值是什么?使用info['rnum']=范围在1到5之间的原始输入添加字典infoHm。。。您是否已经完成了将原始输入的结果转换为整数的过程?不,这会引发另一个错误……我会发布所有代码,但它希望我在这个问题上多写些东西,我真的不知道还能说什么/好的,只是检查一下,如果你想要一个数字,你正在把原始输入的结果转换成一个整数,对吗?而且为什么你已经在con_拥有了价值观?这不是应该由用户输入填写的吗?我建议您再次尝试运行该程序,但将打印信息['rnum']放在导致错误的循环前面。con_owned有9项,rnum只能按照说明介于1和5之间。但1和5之间不是必需的。。。如果您输入100,它将允许,然后您将得到问题;你没有验证数字,但我输入了一个介于1和5之间的数字,仍然得到了错误。我做了你的建议,仍然得到了错误。谢谢你。
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? '))