Python 如何让测验显示学生答案,并根据分数显示正确答案?

Python 如何让测验显示学生答案,并根据分数显示正确答案?,python,arrays,loops,Python,Arrays,Loops,所以我基本上是想复制这个图像,如图所示。我的问题是,每当我运行程序时,它基本上都会告诉我无法运行它。我不确定我的代码中是否有错误的地方。由于“else:”部分的语法错误,每当我运行它时都会出现错误 首先,我要说的是继续学习Python并通过教程进行改进。 我将在代码中的注释中尽可能更好地解释我编写的重构代码,以便您了解这里发生了什么。如果您仍然有疑问,请随时在评论中询问我 getStudentAnswers逻辑在如下函数中定义,我在主代码段中调用该函数,该函数从examAnswers变量开始。缩

所以我基本上是想复制这个图像,如图所示。我的问题是,每当我运行程序时,它基本上都会告诉我无法运行它。我不确定我的代码中是否有错误的地方。由于“else:”部分的语法错误,每当我运行它时都会出现错误


首先,我要说的是继续学习Python并通过教程进行改进。
我将在代码中的注释中尽可能更好地解释我编写的重构代码,以便您了解这里发生了什么。如果您仍然有疑问,请随时在评论中询问我

getStudentAnswers
逻辑在如下函数中定义,我在主代码段中调用该函数,该函数从
examAnswers
变量开始。缩进在python中扮演着重要角色,因此首先运行未缩进的代码,并调用
getStudentAnswers
函数

#获取学生答案的函数
def getStudentAnswers():
listOfAnswers=[]
#运行for循环10次
对于范围(10)中的qNum:
#从用户那里得到答案
打印(“输入问题答案”,qNum+1,”:“,sep=”“,end=”“)
答案=输入()
#将答案附加到列表中
ListofAnwers.append(答案)
#返回最终答案列表
回答者返回列表
#有效考试答案列表
examAnswers=['A','C','A','A','D','B','C','A','C','B']
#变量来保存正确答案和错误答案的计数
countCorrect=0
CountError=0
#从学生那里得到所有答案
studentAnswers=getStudentAnswers()
#运行for循环10次
对于范围(10)内的i:
#如果考试答案与学生答案一致,请打印并更正
如果examAnswers[i]==学生回答[i]:
countCorrect+=1
打印('问题',i+1,'正确!')
#如果考试答案与学生答案不匹配,请打印并更正错误
其他:
打印('问题',i+1,'错误!')
计数错误+=1
#计算遗漏问题的数量和评分,并打印出来
未命中的问题=10-计数正确
等级=10*正确
打印('您错过了',错过了问题,'问题')
打印('您的成绩为:',成绩)
运行代码后,应该会得到如下所示的所需输出

Enter answer to question 1: A
Enter answer to question 2: B
Enter answer to question 3: C
Enter answer to question 4: D
Enter answer to question 5: A
Enter answer to question 6: B
Enter answer to question 7: C
Enter answer to question 8: D
Enter answer to question 9: A
Enter answer to question 10: A
Question 1 is correct!
Question 2 is WRONG!
Question 3 is WRONG!
Question 4 is WRONG!
Question 5 is WRONG!
Question 6 is correct!
Question 7 is correct!
Question 8 is WRONG!
Question 9 is WRONG!
Question 10 is WRONG!
You missed 7 questions.
You grade is: 30

什么是
main()
?另外,您的一些缩进是错误的…我们需要完整的错误和回溯我们还需要代码getstudentanswers@Netwave抱歉,它应该是def main():@MosesMartinez您可以编辑您的问题-您可以这样做以添加其他评论中要求的必要信息吗?非常感谢!这意味着我被这件事困扰了两天!没问题!Python应该是简单的,只要继续努力:)
Enter answer to question 1: A
Enter answer to question 2: B
Enter answer to question 3: C
Enter answer to question 4: D
Enter answer to question 5: A
Enter answer to question 6: B
Enter answer to question 7: C
Enter answer to question 8: D
Enter answer to question 9: A
Enter answer to question 10: A
Question 1 is correct!
Question 2 is WRONG!
Question 3 is WRONG!
Question 4 is WRONG!
Question 5 is WRONG!
Question 6 is correct!
Question 7 is correct!
Question 8 is WRONG!
Question 9 is WRONG!
Question 10 is WRONG!
You missed 7 questions.
You grade is: 30