2014-11-24 71 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

相关问题