之前工作正常后代码抛出错误

时间:2015-03-20 10:23:17

标签: python python-3.x

def main():
    numbers = [
    'No',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine'
'Ten' ]
text_one = 'green bottles\nHanging on the wall'
text_two = "\nAnd if one green bottle\nShould accidentally fall\nThere'll be"
# Each iteration of this loop prints one verse of the song

for i in range(10, 0, -1):         
    file=open('ten.txt', 'a')
    file.write(numbers[i]+' ')
    file.write(text_one+' ')
    file.write(numbers[i]+' ')
    file.write(text_one+' ') 
    file.write(text_two+' ')
    file.write(numbers[i-1]+' ')
    file.write(text_one+' ')
    file.close()

if __name__ == '__main__':
    main()
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 32, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 22, in main
builtins.IndexError: list index out of range

这是有效的,但现在抛出一个错误,谁能看到我搞砸了?

1 个答案:

答案 0 :(得分:3)

您似乎错过了上次参赛作品的逗号:

'Nine' #<- here
'Ten' ]

一旦你看到一个错误,你正在索引一个范围,你可以通过打印长度或调试它来轻松测试你的列表是否具有所需的长度。