【问题标题】:How do I make quiz display student answers and show correct answers with grade?如何让测验显示学生答案并显示正确答案和成绩?
【发布时间】:2019-04-26 03:56:00
【问题描述】:

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

def main():
examAnswers = ['A', 'C', 'A', 'A', 'D', 'B', 'C', 'A', 'C', 'B']
countCorrect = 0
countWrong = 0
studentAnswers = getStudentAnswers()

for i in range(10):
 if examAnswers[i] == studentAnswers[i]:
    print("Congratulations!", countCorrect + 1)
    else:
        print("Wrong!", countWrong + 1)


listOfAnswers = []
for qnum in range(10):
print("Enter answer to question ", qnum+1, ":", sep="", end="")
listofAnswers.append(input())
return listOfAnswers

main()

【问题讨论】:

  • 什么是main()?还有一些你的缩进是错误的......
  • 我们需要完整的错误和回溯
  • 我们还需要getstudentanswers的代码
  • @Netwave 抱歉它应该是 def main():
  • @MosesMartinez 您可以编辑您的问题 - 您可以这样做以添加其他 cmets 中要求的必要信息吗?

标签: python arrays loops


【解决方案1】:

首先,我想说的是继续学习 Python 并通过教程进行改进。
我将在代码中的 cmets 中尽可能好地解释我编写的重构代码,以便您对这里发生的事情有所了解。如果您还有疑问,请随时在 cmets 中问我。

getStudentAnswers 逻辑定义在如下函数中,我在主要代码段中调用该函数,该代码从examAnswers 变量开始。缩进在python中的作用很大,所以先运行没有缩进的代码,然后调用getStudentAnswers函数。

#Function to get the answers of students
def getStudentAnswers():

    listOfAnswers = []

    #Run a for loop 10 times
    for qNum in range(10):

        #Get the answer from the user
        print("Enter answer to question ", qNum + 1, ": ", sep="", end="")
        answer = input()

        #Append the answer to the list
        listOfAnswers.append(answer)

    #Return the final list of answers
    return listOfAnswers

#List of valid exam answers
examAnswers = ['A', 'C', 'A', 'A', 'D', 'B', 'C', 'A', 'C', 'B']

#Variable to hold the count of correct and wrong answers
countCorrect = 0
countWrong = 0

#Get all the answers from the students
studentAnswers = getStudentAnswers()

#Run a for loop 10 times
for i in range(10):

    #If exam answer matches student answer, print it and increment countCorrect
    if examAnswers[i] == studentAnswers[i]:
        countCorrect+=1
        print('Question',i+1,'is correct!')

    # If exam answer does not match student answer, print it and increment countWrong
    else:
        print('Question',i+1,'is WRONG!')
        countWrong+=1

#Calculate number of missedQuestions and grade and print it
missedQuestions = 10 - countCorrect
grade = 10*countCorrect

print('You missed',missedQuestions,'questions.')
print('You grade is:',grade)

一旦你运行代码,你应该得到如下所需的输出。

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

【讨论】:

  • 非常感谢!这意味着我被这件事困住了大约 2 天!
  • 没问题! Python 本来就是简单的,只要继续努力 :)
猜你喜欢
  • 1970-01-01
  • 2014-06-02
  • 2018-06-28
  • 1970-01-01
  • 2018-11-30
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多