我的python代码中有两个列表用于我的任务:
exam_solutions = ['B','B','B','B','B','B','B','B','B','B']
student_answers = []
在我的代码中,我让用户输入多项选择题的答案。然后我将它设置为将他们输入的答案附加到student_answers列表中。我想比较两个列表并让它输出正确的答案,以便我可以在以后显示正确的百分比。
ex:exam_solutions = ['B','B','B','B','B','B','B','B','B','B']
student_answers = ['A','B','B','C','B','B','A','B','B','D']
然后在比较两个列表之后我能输出6个答案是否正确?
答案 0 :(得分:0)
您可以使用zip()
分别汇总每个答案和解决方案,并sum()
计算有多少匹配/正确答案:
@user_array.each_with_index do |candidate, index|
@computer_sequence.each do |computer_number|
if candidate == computer_number && candidate == @computer_sequence[index]
value_and_place +=1
elsif candidate == computer_number && candidate != @computer_sequence[index]
value_only +=1
end
end
end
答案 1 :(得分:0)
correct = sum(x == y for x, y in zip(exam_solutions, student_answers))
x == y
将是True
或False
(分别为1
和0
)。然后我们找到这些的总和,正确答案的数量。
要提高效率,请定义自己的自定义功能:
def correct_over_six(exam_solutions, student_answers):
correct = 0
for x, y in zip(exam_solutions, student_answers):
correct += (x == y)
if correct >= 6:
return True
return False