Python比较两个列表并得到不正确的结果

2024-09-29 23:22:33 发布

您现在位置:Python中文网/ 问答频道 /正文

该计划的目标是比较学生答案列表和正确答案列表,并计算有多少答案是正确的。答案键存储为字符串列表,学生答案从文本文件中读取,然后转换为大写以匹配答案键。你知道吗

#Read student answer file
student_answers = infile.read()
#Convert student answer to all caps
student_answers = [answer.upper() for answer in student_answers]

我的程序运行没有错误,但是比较两个列表的结果是不正确的。只有3个答案是正确的,有20个答案是正确的。下面是到目前为止我已经完成的相关代码,我还包括了输出Program Output Image的屏幕截图。只有问题1、3、5、7和25应标记为不正确。我仔细检查了两个列表,它们包含正确的信息,所以它不是输入错误。任何关于我可能出错的指导都将不胜感激。你知道吗

for studentLine, keyLine in zip(Student, TestKey):
    keyAnswer = keyLine.split()
    studentAnswer = studentLine.split()
    #Compare student answer to test key
    if studentAnswer == keyAnswer:
        correct += 1
        percent_score += 4
        print('Good job! Question ', index + 1, 'is correct!')
        index +=1
    if studentAnswer != keyAnswer:
        incorrect += 1
        incorrect_list.append(index + 1)
        print('The correct answer to question ', index + 1, 'is ', TestKey[index])
        index +=1

Tags: to答案answerin列表forindex错误

热门问题