IndexError ...超出范围,
问题描述:
继续从以前的真棒帮助,我现在有异常处理的问题。 我有11个地块的索引号被选中。如果用户选择高于11,它应该请求他们重新输入但atm,我得到IndexError:列表索引超出范围。 我会认为除了行会处理其他任何东西......但它必须是一个缺失的行?IndexError ...超出范围,
try:
response = raw_input("Select a monitoring plot from the list (0-11): ")
if response == 'q':
confirm = raw_input('Confirm quit (y/ n)...')
if confirm == 'y':
print 'Bye'
break
else:
continue
selected = dataList[int(plotSelect) + 1]
print 'You selected : ', selected[1]
except ValueError:
print "Error: Please enter a number between 0 and 11"
答
except ValueError
only only a ValueError
。您需要添加IndexError
还有:
except (ValueError, IndexError):
+0
是的,就是这样。愚蠢的我......谢谢 – 2013-04-21 04:12:28
答
except ValueError
意味着你只能望尘莫及类型ValueError
的execptions。如果您想要以不同的方式处理它,或者抓住两者并以相同的方式处理,请包含IndexError
的捕获物。
答
ValueError
将拿出当他用户输入的东西是不是数字。 (所以int("hello")
抛出ValueError
)
IndexError
当用户输入的数字大于列表中的元素数时抛出。 (例如,range(5)[7]
你可能想尝试让你先输入一个循环像下面
resp = ""
while resp not in ('0', '1', ... '10', 'q'):
resp = raw_input(...)
那是什么'plotSelect'?它应该是'response'? – Cairnarvon 2013-04-21 03:37:36