我的任务中有一个问题,我使用for循环来比较两个列表,一个是答案键,另一个是学生答案列表,产生一个+4的分数给出正确的响应,-1表示不正确,0表示省略的响应。它工作正常,直到它到达循环的最后一个元素。我知道程序正在“读取”最后的元素,因为我以前打过它们,但它没有正确调整分数,实际上是减去一个点,无论答案如何。我无法弄清楚出了什么问题,特别是因为它只是最后一部分。
以下是我的代码的相关部分:
#As we go over each line, we add to the student counter to keep track of the number of students.
#We use the split function to turn each line of the file into a list that we can use.
for line in open_file:
students += 1
student_ans = line.split(",")
#The score tracker keeps track of total score.
score_tracker = 0
#This for loop iterates over each value in the list, comparing to the answer key.
for n in range(number_ans):
a = answerkey[n]
b = student_ans[n+1]
#A series of if/elif/else statements give and take points according to answers.
if a == b:
score_tracker += 4
elif b == "":
score_tracker += 0
else:
score_tracker -= 1
print(score_tracker)
任何帮助都将不胜感激。