Я создаю программу, которая просит пользователя ввести ответы на вопросы с несколькими вариантами ответов, а затем они предоставляют ключ ответа. После этого программа оценивает экзамен, дает им оценку и сообщает, какой номер вопроса они ошиблись, каков был их ответ и каков правильный ответ.
Вот примерный прогон моей программы :
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 критерия:
- Номер вопроса, который они ошиблись
- Какой ответ они ответили
- Какой правильный ответ
Я попытался получить номер вопроса через .index из списка, а также итератор для ответа и ответа о том, что программа работала при оценке текста (p и q).
Любой и все решения приветствуются! Также, если у вас есть лучшее название программы, чем у gradermati c, я бы хотел услышать это!