嘿,前几天我在乱搞一些代码,它给了我这个错误
if clearargs[1] == "history": \n IndexError: list index out of range
elif startswith(ui, "clear") or startswith(ui, "clr"):
clearargs = ui.split()
if len(clearargs) < 1:
refs.clearscr("Windows", os)
else:
if clearargs[1] == "history":
history = []
os.remove(hfilepath)
elif clearargs[1] == "exthistory":
extendedhistory = []
os.remove(ehfilepath)
顺便说一句,大多数函数都是自定义的
else块将仅在
len(clearargs < 1)
时执行,因此唯一有效的索引是clearargs[0]
。这是一个逻辑错误列表索引从零开始
因此,如果列表的长度正好是一,那么
clearargs[0]
就是对列表中第一个值的正确访问当要求从
clearargs[1]
获取值时,假设列表的长度至少为2相关问题 更多 >
编程相关推荐