首页 > 解决方案 > 我正在尝试创建一个使用文本文件并计算分数的测验程序。但是分数不起作用

问题描述

我正在创建一个 python 测验程序,它使用两个文本文件,即一个用于问题,一个用于答案。

1|What is the colour of mango ?|a.Yellow b.Blue c.Black d.Brown
2|What is the colour of apple ?|a.Yellow b.Red c.Black d.Brown
3|What is the colour of chochlate ?|a.Yellow b.Blue c.Black d.Brown
4|What is the colour of water ?|a.Yellow b.Blue c.Black d.Brown

问题.txt

1|a
2|b
3|d
4|b

答案.txt

该程序还应该计算分数,即如果正确+1,如果错误则什么也不做。但是分数计数器没有给出正确的结果。这是代码

QuestionsText = open("Questions.txt",'r')
AnswersText = open("Answers.txt",'r')
Questions = QuestionsText.readlines()
Answers = AnswersText.readlines()
Score = 0
i=0
while i<4 :
  for Question in Questions:
    for Answer in Answers:
      AnswerPart = Answer.split('|')
      AnswerNumber = AnswerPart[0]
      RightAnswer = AnswerPart[1]
    QuestionPart = Question.split('|')
    QuestionNumber = QuestionPart[0]    
    ActualQueston = QuestionPart[1]
    Options = QuestionPart[2]
    print (QuestionNumber,'.',ActualQueston)
    print (' ',Options)    
    UserAnswer = input("Your Answer is = ")
    print('SUBMITTED')
    if UserAnswer == RightAnswer :      
      Score = Score + 1
      i=i+1
    else :
      i = i + 1
             
print("Your Score is =",Score)

这次我尝试运行一些测试来确定实际问题的出处。我在每次迭代时都打印了正确的答案,只是为了确定它是否正确地取值。

QuestionsText = open("Questions.txt",'r')
AnswersText = open("Answers.txt",'r')
Questions = QuestionsText.readlines()
Answers = AnswersText.readlines()
Score = 0
i = 0
while i < 4:
  for Question in Questions:
    for Answer in Answers:
      AnswerPart = Answer.split('|')
      AnswerNumber = AnswerPart[0]
      RightAnswer = AnswerPart[1]
      print("Test 0",AnswerNumber,RightAnswer)
      QuestionPart = Question.split('|')
      QuestionNumber = QuestionPart[0]    
      ActualQueston = QuestionPart[1]
      Options = QuestionPart[2]
      print("Test 1",AnswerNumber,RightAnswer)
      print (QuestionNumber,ActualQueston,Options)
      UserAnswer = input( " Your Answer is : " )
      print(UserAnswer)
      print(RightAnswer)
      if UserAnswer == RightAnswer :
        print("Test 2",AnswerNumber,RightAnswer)
        print('Right')
        Score = Score + 1
        print("R",Score)
        i = i + 1
      else :
        print("Test 3",AnswerNumber,RightAnswer)
        print('Wrong')
        print("W",Score)
        i = i + 1
        break      
print("Your Score is =",Score)

所以在 if 语句的最后,每个值都是正确的,但是在它之后,尽管用户答案和正确答案都是相同的,但它仍然说错了。

标签: python

解决方案


您的 for 循环是问题所在。代码本身会迭代所有答案,因此 RightAnswer 始终相同。对于您提供的答案,它是b. 因此,您检测到错误的答案并因此得到错误的分数。

编辑:一个应该工作的实现是例如:

# Assumption 1: every line in Questions.txt is one set of question and possible answers
# Assumption 2: every line in Answers.txt is one set of question number and correct answer
# Assumption 3: the order of questions in Answers.txt and Questions.txt is the same
QuestionsText = open("Answers.txt",'r')
AnswersText = open("Questions.txt",'r')
Score = 0
i = 0
# iterate over both files simultaneously
# q and a are one line in the regarding file
for q,a in zip(QuestionsText,AnswersText):
    # split q in number, question and options
    question_number,question,options = q[:-1].split("|")
    # split a in number and correct answer
    solution_number,solution = a.split("|")

    # start the test by printing in console
    # print the question
    print(f"Question {question_number}:\n{question}")
    #print the options
    print(options)
    useranswer = input("What is your answer? Please type in the letter of the option and press enter\n")
    # upper makes it case insensitive
    if solution.upper().startswith(useranswer.upper()):
        Score += 1
        print("Your answer is correct.")
    else:
        print(f"Your answer is wrong. The correct answer ist {solution}.")
    i += 1
print(f"You answered {Score} of {i} questions correct.")

这给了你:

Question 1:
What is the colour of mango ?
a.Yellow b.Blue c.Black d.Brown
What is your answer? Please type in the letter of the option and press entera
Your answer is correct.
Question 2:
What is the colour of apple ?
a.Yellow b.Red c.Black d.Brown
What is your answer? Please type in the letter of the option and press enterb
Your answer is correct.
Question 3:
What is the colour of chochlate ?
a.Yellow b.Blue c.Black d.Brown
What is your answer? Please type in the letter of the option and press enterd
Your answer is correct.
Question 4:
What is the colour of water ?
a.Yellow b.Blue c.Black d.Brow
What is your answer? Please type in the letter of the option and press entera
Your answer is wrong. The correct answer ist b.
You answered 3 of 4 questions correct.

推荐阅读