帮助用 Python 创建考试评分程序

发布于 2024-10-31 21:24:34 字数 1021 浏览 1 评论 0原文

我正在尝试创建一个程序,从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼睛不忧郁 2024-11-07 21:24:34

问题是您没有正确嵌套循环。

该循环首先运行,最后将 answer 设置为answerKey 的最后一行。

for line in answerKey:
    answer = line.split()

StudentExam: 中的 for 行: 循环随后运行,但 answer 在此循环中不会更改并将保持不变。

解决方案是使用 zip 组合循环:

for answerLine, studentLine in zip(answerKey, studentExam):
    answer = answerLine.split()
    studentAnswer = studentLine.split()

另外,使用完文件后记得关闭它们:

answerKey.close()
studentExam.close()

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.

for line in answerKey:
    answer = line.split()

The for line in studentExam: loop runs afterwards, but answer doesn't change in this loop and will stay the same.

The solution is combining the loops using zip:

for answerLine, studentLine in zip(answerKey, studentExam):
    answer = answerLine.split()
    studentAnswer = studentLine.split()

Also, remember to close the files when you're done with them:

answerKey.close()
studentExam.close()
葬花如无物 2024-11-07 21:24:34

问题难道不是您迭代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?

别挽留 2024-11-07 21:24:34

您在 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文