用python制作一个考试评分器[列出相关问题]



我正在制作一个程序,要求用户在回答多项选择题时输入,然后他们提供答案键。之后,该程序对考试进行评分,给他们一个分数,然后告诉他们错了什么问题,他们的回答是什么,正确答案是什么

以下是我的程序运行示例:

Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 1
How many questions are there? >> 3
What is your answer for question #1 >> a
What is your answer for question #2 >> a
What is your answer for question #3 >> a
Responses:
1. a
2. a
3. a
Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 2
What is the correct answer for question #1 >> a
What is the correct answer for question #2 >> c
What is the correct answer for question #3 >> d
Responses:
1. a
2. c
3. d
Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 3
Traceback (most recent call last):
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 99, in <module>
a.start()
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 9, in start
a.enterResponses()
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 38, in enterResponses
a.start() # take us back to the main menu
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 12, in start
a.answerKey()
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 55, in answerKey
a.start()
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 15, in start
a.grade()
File "/Users/faiqashraf/Desktop/github site/PersonalProjects/grader/grader.py", line 68, in grade
goingIn = "#" + str(self.responses.index(p)) + " user entered: " + str(self.responses(p))+ " correct answer: " + str(self.answers(q))
TypeError: 'list' object is not callable

这是我的代码:

class Grader:
def start(self):
# greeting message
o = input("Welcome to gradermatic.nTo enter responses, enter 1.nTo enter an answer key, enter 2.nTo grade, enter 3.nPress any other key to quit. >> ")
if o == "1":
# enter responses
self.numOfQuestions = input("How many questions are there? >> ")
a.enterResponses()
elif o == "2":
# enter answer key
a.answerKey()
elif o == "3":
# grade the exam
a.grade()
# get number of questions
def enterResponses(self):
# start gathering answers
i = 1
self.responses = []
while i <= int(self.numOfQuestions):
entry = input("nWhat is your answer for question #" + str(i) + " >> ")
self.responses += entry
i += 1
# display user responses
print("nResponses:n")
j = 1
for r in self.responses:
print(str(j) + ". " + r)
j+=1
# change any answers?
# change = input("Would you like to change any answers? 1 = yes, 2 = no")
# if change == "1":
#     a.changeAns()
a.start() # take us back to the main menu
def answerKey(self):
# input answer key
x = 1
self.answers = []
while x <= int(self.numOfQuestions):
aentry = input("nWhat is the correct answer for question #" + str(x) + " >> ")
self.answers += aentry
x += 1
# display answer key
print("nResponses:n")
y = 1
for z in self.answers:
print(str(y) + ". " + z)
y+=1
a.start()
def grade(self):
# grade the responses
# time to actually grade the exam
numCorrect = 0
self.incorrect = []
for p, q in zip(self.responses, self.answers):
if p == q:
# correct answer, so add 1 to their score
numCorrect += 1
else:
# incorrect answer, note:the question number                           user entry                                  correct answer
goingIn = "#" + str(self.responses.index(p)) + " user entered: " + str(self.responses(p))+ " correct answer: " + str(self.answers(q))
self.incorrect += goingIn
goingIn = "" # reset, incase we need to add more stuff

# issue a grade
print(str(numCorrect))
print("Number of correct answers = " + str(numCorrect) + " out of " + str(self.numOfQuestions))
grade = int(numCorrect) / int(self.numOfQuestions)
print("Your score is: " + str(grade))
# display incorrect answers:
print("Here are the questions the user got wrong, as well as their correct answers:")
for l in self.incorrect:
print(l)
# end the program
exit(0)
# def changeAns(self):
#     # time to change answers
#     whatToChange = input("What question's answer do you want to change? Enter a number between 1 and " + str(numOfQuestions))
#     if whatToChange <= int(numOfQuestions):
#         # change the answer
#         newAns = input("What do you want the answer to be? >> ")
#         responses[whatToChange] == newAns

if __name__ == "__main__":
a = Grader()
a.start()

我的问题是:我如何记下告诉用户他们错了问题的3个标准?回想一下,这3个标准是:

  1. 他们弄错的问题编号
  2. 他们提供了什么答案作为回应
  3. 正确答案是什么

我尝试通过.index从列表中获取问题编号,以及程序在对文本(p和q(进行评分时所使用的响应和答案的迭代器。

欢迎任何解决方案!另外,如果你有比gradermatic更好的程序名称,我很想听!

这里有几个问题。首先,a是您为Grader类的实例指定的变量名。它不应该在类本身内部被引用,而是使用self。您收到的错误来自此行,因为您尝试将列表视为函数。

goingIn = "#" + str(self.responses.index(p)) + " user entered: " + str(self.responses(p))+ " correct answer: " + str(self.answers(q))

您已经在列表上迭代了,所以您所需要做的就是打印pq:

goingIn = "#" + str(self.responses.index(p)) + " user entered: " + p + " correct answer: " + q

最后,行self.incorrect += goingIn的行为将类似于list(str),并为字符串中的每个字母创建一个单独的列表条目。相反,您应该使用append()列表方法。

self.incorrect.append(goingIn)

现在它将运行到完成。

Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 1
How many questions are there? >> 3
What is your answer for question #1 >> a
What is your answer for question #2 >> a
What is your answer for question #3 >> a
Responses:
1. a
2. a
3. a
Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 2
What is the correct answer for question #1 >> a
What is the correct answer for question #2 >> b
What is the correct answer for question #3 >> a
Responses:
1. a
2. b
3. a
Welcome to gradermatic.
To enter responses, enter 1.
To enter an answer key, enter 2.
To grade, enter 3.
Press any other key to quit. >> 3
2
Number of correct answers = 2 out of 3
Your score is: 0.6666666666666666
Here are the questions the user got wrong, as well as their correct answers:
#0 user entered: a correct answer: b

这是经过编辑的Grader类。

class Grader:
def start(self):
o = input("Welcome to gradermatic.nTo enter responses, enter 1.nTo enter an answer key, enter 2.nTo grade, enter 3.nPress any other key to quit. >> ")
if o == "1":
self.numOfQuestions = input("How many questions are there? >> ")
self.enterResponses()
elif o == "2":
self.answerKey()
elif o == "3":
self.grade()
def enterResponses(self):
i = 1
self.responses = []
while i <= int(self.numOfQuestions):
entry = input("nWhat is your answer for question #" + str(i) + " >> ")
self.responses += entry
i += 1
print("nResponses:n")
j = 1
for r in self.responses:
print(str(j) + ". " + r)
j+=1
self.start() # take us back to the main menu
def answerKey(self):
x = 1
self.answers = []
while x <= int(self.numOfQuestions):
aentry = input("nWhat is the correct answer for question #" + str(x) + " >> ")
self.answers += aentry
x += 1
print("nResponses:n")
y = 1
for z in self.answers:
print(str(y) + ". " + z)
y+=1
self.start()
def grade(self):
numCorrect = 0
self.incorrect = []
for p, q in zip(self.responses, self.answers):
if p == q:
numCorrect += 1
else:
goingIn = "#" + str(self.responses.index(p)) + " user entered: " + p + " correct answer: " + q
self.incorrect.append(goingIn)
goingIn = "" # reset, incase we need to add more stuff

# issue a grade
print(str(numCorrect))
print("Number of correct answers = " + str(numCorrect) + " out of " + str(self.numOfQuestions))
grade = int(numCorrect) / int(self.numOfQuestions)
print("Your score is: " + str(grade))
print("Here are the questions the user got wrong, as well as their correct answers:")
for l in self.incorrect:
print(l)
exit(0)

最新更新