Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
帮助在Python中创建考试评分程序_Python_List_Split_Line - Fatal编程技术网

帮助在Python中创建考试评分程序

帮助在Python中创建考试评分程序,python,list,split,line,Python,List,Split,Line,我试图创建一个程序,从txt文件中读取多项选择答案,并将其与设置的答案键进行比较。这就是我目前所知道的,但问题是,当我运行它时,在程序的整个生命周期中,答案键卡在一个字母上。我在for answerKey行后面放了一个打印语句,它正确地打印了出来,但是当它将“考试”答案与答案键进行比较时,它被卡住了,总是认为“a”应该是正确的答案。这很奇怪,因为这是我示例答案密钥中的第三个条目 代码如下: answerKey = open("answerkey.txt" , 'r') studentExam =

我试图创建一个程序,从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!")
answerKey=open(“answerKey.txt”,“r”)
studentExam=open(“studentExam.txt”,“r”)
索引=0
numCorrect=0
对于answerKey中的行:
答案=line.split()
对于studentExam中的行:
studentAnswer=line.split()
如果学生回答!=答复:
打印(“您得到了问题编号”,索引+1,“错误\n正确答案是”,答案是,“但您回答了”,学生回答)
指数+=1
其他:
numCorrect+=1
指数+=1
等级=整数((numCorrect/20)*100)
打印(“正确回答的问题数:”,numCorrect)
打印(“回答错误的问题数量:”,20-numCorrect)
打印(“您的成绩为”,成绩,%”)

如果等级则在for-line循环的每次迭代中覆盖您的答案。A很可能是答案键中的最后一个条目。尝试将两个for循环组合为一个

在for-line循环的每次迭代中,您都会覆盖您的答案。A很可能是答案键中的最后一个条目。尝试将两个for循环组合为一个

问题不在于您迭代了answerkey.txt中的所有行,然后将其最后一行仅与所有studentexam.txt行进行比较吗?

问题不在于您迭代了answerkey.txt中的所有行,然后将其最后一行仅与所有studentexam.txt行进行比较吗没有正确嵌套循环

此循环首先运行,最后设置应答键最后一行的
answer

for line in answerKey:
    answer = line.split()
studentExam:
中的
for行循环随后运行,但
answer
在此循环中不会更改,并将保持不变

解决方案是使用以下方法组合循环:

此外,请记住在处理完文件后关闭这些文件:

answerKey.close()
studentExam.close()

问题是没有正确嵌套循环

此循环首先运行,最后设置应答键最后一行的
answer

for line in answerKey:
    answer = line.split()
studentExam:
中的
for行循环随后运行,但
answer
在此循环中不会更改,并将保持不变

解决方案是使用以下方法组合循环:

此外,请记住在处理完文件后关闭这些文件:

answerKey.close()
studentExam.close()

文本文件的格式是什么?请提供示例输入文件以及使用这些输入文件运行程序时获得的输出。您能否添加一些示例
answerkey.txt
studentexam.txt
文件。我认为问题在于您正在比较两个数组(
studentAnswer!=answer
)文本文件的格式是什么?请提供示例输入文件以及使用这些输入文件运行程序时得到的输出。您可以添加一些示例
answerkey.txt
studentexam.txt
文件吗。我认为问题在于您正在比较两个数组(
studentAnswer!=answer
),而不是它们的内容。。。