2014-11-24 72 views
0

所以現在,我得到了[3,3,3 ...] 18個值,這很好,因爲這意味着我的if和for循環運行良好。唯一的問題是,我似乎無法弄清楚如何讓for循環跳過不同的元素。使用兩個列表來比較元素並在這兩個列表中創建一個非相同元素的列表?

'#該程序比較兩個平行列表,以評分 '#多選題考試。一個列表有考試解決方案 '#,第二個列表有一個學生的答案。 '# '#在第三個列表中存儲每個錯過問題的問題編號 '#。

'#您必須使用解決方案中提供的三個列表。 '#您的解決方案必須使用索引 '#不要編寫任何其他用戶定義函數 '# - 將所有代碼寫入主函數。 '#你可以不嵌入列表[]內的Python編程語句

主要功能

def main(): 

# do not modify this statement or contents of list 
exam_solution = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\ 
      'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] 

# do not modify this statement or contents of list 
student_answers = ['B', 'D', 'B', 'A', 'C', 'A', 'A', 'A', 'C', 'D', 'B', 'C',\ 
      'D', 'B', 'D', 'C', 'C', 'B', 'D', 'A'] 

# do not modify this statement, you must begin with an empty list 
questions_missed = [] 

questions = int(len(exam_solution)) 
print(questions) 
i= 0 

for answers in range(0, questions): 
    if exam_solution[i] == student_answers[i]: 
     i += 1 
    else: 
     questions_missed.append(i+1) 


print(questions_missed) 



input('press enter to continue') 


main() 

' ##你的輸出應類似於以下內容: '## ' ##恭喜!你通過考試

「##你答對17題和第3題正確

」 ##,你答錯問題的數字是:3 7 14

「##按Enter鍵continue`

回答

0
if exam_solution[i] == student_answers[i]: 
    i += 1 
else: 
    questions_missed.append(i+1) 

大概應該是

if exam_solution[answers] == student_answers[answers]: 
    i += 1 
else: 
    questions_missed.append(answers + 1) 

這是一個很好的教訓,當你不選擇有意義的變量名時會發生什麼

+0

這樣一個簡單的修復程序,使整個程序的工作。非常感謝。 – Rsherrill 2014-11-24 02:49:11

相關問題