我對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:'這不是做你認爲它是做什麼 –
有沒有問題? –