我正在创建一个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
Questions.txt
1|a
2|b
3|d
4|b
Answers.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语句的结尾,每个值都被正确地获取,但是在它之后,尽管useranswer和right answer都是相同的,但它仍然表示错误
问题就出在你的for循环中。代码会对所有答案进行迭代,因此RightAnswer总是相同的。您提供的答案是
b
。 所以你发现了错误的答案,所以得到了错误的分数编辑: 应该有效的实现是,例如:
这将为您提供:
相关问题 更多 >
编程相关推荐