该程序的目标是将学生答案列表与正确答案列表进行比较,并计算正确答案的数量。答案键存储为字符串列表,学生的答案从文本文件中读取,然后转换为大写以匹配答案键。
#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
我认为您错误地拆分而不是剥离。
所以你应该使用:
keyAnswer = keyLine.strip()
studentAnswer = studentLine.strip()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句