输入:
AEList =[]
cc = infile.readline().strip()
while cc != 'EXIT':
if cc[0] == '3':
name = 'A'
AEList.append(cc)
cc = infile.readline().strip()
输出:
Traceback (most recent call last):
File "/Users/alexleblanc/Documents/pa8.py", line 81, in <module>
main()
File "/Users/alexleblanc/Documents/pa8.py", line 47, in main
if cc[3] == '3' :
IndexError: string index out of range
答案 0 :(得分:0)
一种解决方案是在检查特定值之前检查字符串/数组是否为空:
if cc and cc[0] == '3'
或
if len(cc) > 0 and cc[0] == '3'
请记住,如果您尝试访问不存在的索引中的值,则会出错。