我正在尝试制作一个程序而且我收到此错误
Traceback (most recent call last):
File "E:\MyAi.py", line 27, in <module>
ask = raw_input(statements[randint(0,len(answers) - 1)])
IndexError: list index out of range
这是我的代码:
if ask[len(ask) - 1] == "?":
questions.append(ask)
ask = raw_input(answers[randint(0,len(answers) - 1)])
elif ask[len(ask) - 1] == "!":
answers.append(ask)
ask = raw_input(statements[randint(0,len(answers) - 1)])
else:
statements.append(ask)
ask = raw_input(questions[randint(0,len(answers) - 1)])
为什么我的代码会导致此问题?我无法弄清楚原因。
答案 0 :(得分:0)
您可能打算这样做:
ask = raw_input(statements[randint(0,len(statements) - 1)])
还有这个:
ask = raw_input(questions[randint(0,len(questions) - 1)])
否则,如果answers
比您的其他列表更大,则可能会遇到错误。