帮助用 Python 创建考试评分程序
我正在尝试创建一个程序,从 txt 文件中读取多项选择答案并将它们与设定的答案键进行比较。这是我到目前为止所拥有的,但问题是,当我运行它时,答案键在程序的整个生命周期中都停留在单个字母上。我在 for answerKey 行之后放置了一条打印语句,它可以正确打印出来,但是当它将“考试”答案与答案键进行比较时,它会卡住并始终认为“A”应该是正确答案。这很奇怪,因为这是我的示例答案中的第三个条目。
这是代码:
answerKey = open("answerkey.txt" , 'r')
studentExam = open("studentexam.txt" , 'r')
index = 0
numCorrect = 0
for line in answerKey:
answer = line.split()
for line in studentExam:
studentAnswer = line.split()
if studentAnswer != answer:
print("You got question number", index + 1, "wrong\nThe correct answer was" ,answer , "but you answered", studentAnswer)
index += 1
else:
numCorrect += 1
index += 1
grade = int((numCorrect / 20) * 100)
print("The number of correctly answered questions:" , numCorrect)
print("The number of incorrectly answered questions:" , 20 - numCorrect)
print("Your grade is" ,grade ,"%")
if grade <= 75:
print("You have not passed")
else:
print("Congrats! You passed!")
感谢您给我的任何帮助!
I am trying to create a program that reads multiple choice answers from a txt file and compares them to a set answer key. This is what I have so far but the problem is that that when I run it, the answer key gets stuck on a single letter throughout the life of the program. I have put a print statement right after the for answerKey line and it prints out correctly, but when it compares the "exam" answers to the answer key it gets stuck and always thinks "A" should be the correct answer. Which is weird because it is the 3rd entry in my sample answer key.
Here's the code:
answerKey = open("answerkey.txt" , 'r')
studentExam = open("studentexam.txt" , 'r')
index = 0
numCorrect = 0
for line in answerKey:
answer = line.split()
for line in studentExam:
studentAnswer = line.split()
if studentAnswer != answer:
print("You got question number", index + 1, "wrong\nThe correct answer was" ,answer , "but you answered", studentAnswer)
index += 1
else:
numCorrect += 1
index += 1
grade = int((numCorrect / 20) * 100)
print("The number of correctly answered questions:" , numCorrect)
print("The number of incorrectly answered questions:" , 20 - numCorrect)
print("Your grade is" ,grade ,"%")
if grade <= 75:
print("You have not passed")
else:
print("Congrats! You passed!")
Thanks for any help you can give me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您没有正确嵌套循环。
该循环首先运行,最后将
answer
设置为answerKey 的最后一行。StudentExam: 中的
for 行:
循环随后运行,但answer
在此循环中不会更改并将保持不变。解决方案是使用
zip
组合循环:另外,使用完文件后记得关闭它们:
The problem is that you're not nesting the loops properly.
This loop runs first, and ends up setting
answer
to the last line of the answerKey.The
for line in studentExam:
loop runs afterwards, butanswer
doesn't change in this loop and will stay the same.The solution is combining the loops using
zip
:Also, remember to close the files when you're done with them:
问题难道不是您迭代answerkey.txt 中的所有行,然后最终仅将其最后一行与所有studentexam.txt 行进行比较吗?
Wouldn't the problem be that you iterate over all the lines in answerkey.txt and then end up comparing its last line only to all studentexam.txt lines?
您在 for 行循环的每次迭代中都会覆盖您的答案。 A 很可能是答案中的最后一个条目。尝试将两个 for 循环合并为一个!
You are overwriting your answer in every iteration of the for line loop. A is most likely the last entry in your answer key. Try combining the two for loops into just one!