Python 错误:列表索引超出范围?
所以我一直在尝试制作一个python应用程序。它会询问用户想要多少个测验问题,然后他们浏览并键入问题和答案,然后有人回答。 我得到的实际错误在结果中 注意-def有8个空格,因此可以组成代码块Python 错误:列表索引超出范围?,python,python-3.4,Python,Python 3.4,所以我一直在尝试制作一个python应用程序。它会询问用户想要多少个测验问题,然后他们浏览并键入问题和答案,然后有人回答。 我得到的实际错误在结果中 注意-def有8个空格,因此可以组成代码块 from time import sleep def results(): n = 0 score = 0 while n <= times: if user_answers[n] is answer_list[n]: score
from time import sleep
def results():
n = 0
score = 0
while n <= times:
if user_answers[n] is answer_list[n]:
score += 1
n += 1
if user_answers[n] != answer_list[n]:
n += 1
if n > times:
print(" ")
print("Results-")
print("You got:", score, "Correct")
sleep(4)
main()
def quiz():
print(" ")
global user_answers
user_answers = []
number = 0
n1 = 1
while n1 <= times:
print(" ")
print("Question: " + array_list[number])
user_answers.append(input("Answer: "))
n1 += 1
number += 1
if n1 > times:
results()
def answers():
print(" ")
print("Great, now you will need to enter\nall of the answers!")
sleep(4)
global answer_list
answer_list = []
n1 = 1
number = 0
while n1 <= times:
print(" ")
print("Question: " + array_list[number])
answer_list.append(input("Real Answer: "))
n1 += 1
number += 1
if n1 > times:
print(" ")
print("Perfect, now have fun with your quiz!")
print("--------------------------------------")
sleep(4)
quiz()
def main():
print("----------------------------------------")
print("Enter the number of questions you want")
global times
times = int(input("Number: "))
n1 = 1
global array_list
array_list = []
print(" ")
print("Now enter the Questions")
print(" ")
while n1 <= times:
array_list.append(input("Question: "))
n1 += 1
if n1 > times:
answers()
main()
从时间导入睡眠
def results():
n=0
分数=0
而n次:
打印(“”)
打印(“结果-”)
打印(“你得到:”,分数,“正确”)
睡眠(4)
main()
def quick():
打印(“”)
全球用户的回答
用户回答=[]
数字=0
n1=1
而n1次:
结果()
def answers():
打印(“”)
打印(“很好,现在您需要输入\n所有答案!”)
睡眠(4)
全球答案列表
答案列表=[]
n1=1
数字=0
而n1次:
打印(“”)
打印(“太好了,现在让你的测验充满乐趣!”)
打印(---------------------------------------------)
睡眠(4)
测验()
def main():
打印(“-----------------------------------------------”)
打印(“输入所需的问题数量”)
环球时报
时间=整数(输入(“数字:”)
n1=1
全局数组列表
数组_列表=[]
打印(“”)
打印(“现在输入问题”)
打印(“”)
而n1次:
答案()
main()
这是我得到的错误-
Traceback (most recent call last):
File "/Users/isabella/Desktop/programming/Python coding/quiz /testing.py", line 160, in <module>
main()
File "/Users/isabella/Desktop/programming/Python coding/quiz /testing.py", line 157, in main
answers()
File "/Users/isabella/Desktop/programming/Python coding/quiz /testing.py", line 139, in answers
quiz()
File "/Users/isabella/Desktop/programming/Python coding/quiz /testing.py", line 117, in quiz
results()
File "/Users/isabella/Desktop/programming/Python coding/quiz /testing.py", line 8, in results
if user_answers[n] == answer_list[n]:
IndexError: list index out of range
回溯(最近一次呼叫最后一次):
文件“/Users/isabella/Desktop/programming/Python coding/quick/testing.py”,第160行,在
main()
文件“/Users/isabella/Desktop/programming/Python coding/quick/testing.py”,第157行,主文件
答案()
文件“/Users/isabella/Desktop/programming/Python coding/quick/testing.py”,第139行,答案
测验()
文件“/Users/isabella/Desktop/programming/Python coding/quick/testing.py”,quick中的第117行
结果()
文件“/Users/isabella/Desktop/programming/Python coding/quick/testing.py”,第8行,在results中
如果用户回答[n]==回答列表[n]:
索引器:列表索引超出范围
请修复缩进。正确的缩进对于Python程序至关重要。粘贴您的原始代码,然后使用SO编辑器中的{}
工具将其标记为代码块。感谢timrau,巨大的帮助
while n <= times: