我对python相当陌生,刚开始这个学期的课程。我努力想办法编写一个代码,将正确的答案存储为列表,然后从txt文件中读取20个问题中的每一个的学生答案,并将答案存储在另一个列表中。之后,我想比较列表,然后打印答案,程序将显示一条消息,指示学生是否通过(15或更大的正确是通过),总数正确,总数不正确。所以例如正确答案为A,C,A,A,D,B,C,A,C,B,A,D,C,A,D,C,B,B,D,A。学生的答案只是创建自己的文本文件来测试。任何帮助,将不胜感激我目前的格式似乎并不奏效,如下所示。使用Python比较列表?
高清的main():
total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
'B', 'C', 'A', 'C', 'B',\
'A', 'D', 'C', 'A', 'D',\
'C', 'B', 'B', 'D', 'A']
student_answers = open('student_solution.txt', 'r')
for answer in student_answers:
print(answer.strip())
while index in answers == student_answers:
if student_answers[0] == answers[0]:
total +=1
else:
total +=0
student_answers.close()
print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)
if total >= 15:
print('Congratulations! You passed the exam.')
else:
print('Sorry, you have failed the exam.')
的main()
这里是更新的程序仍然似乎给问题。我使用的是学生的答案是 ACAADBCACBADCADCBBDAC AADBCACBADCADCBBDD
高清的main():
total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
'B', 'C', 'A', 'C', 'B',\
'A', 'D', 'C', 'A', 'D',\
'C', 'B', 'B', 'D', 'A']
infile = open('student_solution.txt', 'r')
student_answers = infile.readline()
infile.close()
print(student_answers)
for answer in student_answers:
for y in range(len(answer)):
if answer[y] == answers[y]:
total += 1
print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)
if total >= 15:
print('Congratulations! You passed the exam.')
else:
print('Sorry, you have failed the exam.')
的main()
'而指数的答案== student_answers:'这不是做你认为它是做什么 –
有没有问题? –