2017-04-24 98 views
2

我對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()

+1

'而指數的答案== student_answers:'這不是做你認爲它是做什麼 –

+0

有沒有問題? –

回答

2

可以計算total這樣

total = 0 
for stdnt_ans,correct_ans in zip(student_answers, answers): 
    if stdnt_ans == correct_ans: 
     total += 1 
荏苒兩個列表

它比增加速度快2倍以上total在這個更緊湊但更慢的方式:

total += int(stdnt_ans == correct_ans) 
0

這將得到您的總和。只需要調整你做循環的方式。

def 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 = [[ 'A', 'C', 'A', 'A', 'D',\ 
       'B', 'C', 'D', 'C', 'B',\ 
       'A', 'D', 'C', 'A', 'D',\ 
       'C', 'B', 'A', 'D', 'A'],\ 
       [ 'A', 'C', 'D', 'D', 'D',\ 
       'A', 'C', 'A', 'D', 'B',\ 
       'D', 'D', 'C', 'A', 'D',\ 
       'B', 'B', 'B', 'D', 'A']] 

    for answer in student_answers: 
     for i in range(len(answer)): 
      if answer[i] == answers[i]: 
       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.') 
     total = 0 

main() 
+0

我還刪除了文件方面,以創建要使用的內容。您可以將student_answers替換爲原來的樣子。 – Aklys

+0

我試過你的方法,它似乎有所幫助,現在只有問題是當我爲我創建的.txt文件創建readlines時,它只是通過垂直與水平方向發佈的答案保存記事本,並且僅通過複製回答教授提供的測試並粘貼到記事本中。當試圖處理它粘貼在那裏水平說它的範圍已經達到它的最大值...我猜這是把這些答案作爲一個元素與20?但是我也通過使用infile = open('student_solution.txt','r')和student_answers = infile.readlines()來改變我的格式。 –

+0

然後你有兩種方法可以解決這個問題。您可以將每個文件轉換爲列表,也可以將每行轉換爲列表。這真的取決於整個設置。上面的代碼只是爲了讓你瞭解如何處理列表比較。您可以單獨創建列表,然後像上面那樣比較它們,或者可以將它作爲內聯比較來進行,即從源代碼讀取每個元素。這真的取決於你所得到的數據結構。 – Aklys

-1
for index, value in enumerate(answers): 
    total += int(value == student_answers[index]) 

print('Pass' if total >= 15 else 'Fail') 
+0

雖然這段代碼片段是受歡迎的,並且可能會提供一些幫助,但它會[如果它包含解釋](/ meta.stackexchange.com/q/114762)*如何解決該問題將會[大大改進。沒有這些,你的答案就沒有什麼教育價值了 - 記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –