这个for循环将为用户提供多个响应,其中包含来自单选按钮的值,其值为1,3,5,2,1等....
for i in range(1, 10):
resp_i = form.getvalue('opt_%d' %i, '0')
resp[i] = resp_i
print resp[i]
output of this loop: 3 4 1 2 1 0 0 0 0
此代码包含存储在数据库中的实际答案,其值为1,2,3,5,2同样....... row [0]包含问题ID,行[1]包含答案< / p>
for row in prsnobj.result:
ansdb = {row[0] : row[1]}
print ansdb
output: {1L: 3L} {2L: 4L} {3L: 2L} {4L: 2L} {5L: 2L}
现在我的问题是我想将用户响应与存储在数据库中的实际答案进行比较。我这样做吗?
我试过的代码......并不可行
res1= int(form.getvalue('opt_1', '0'))
res2 = int(form.getvalue('opt_2', '0'))
res3 = int(form.getvalue('opt_3', '0'))
res4 = int(form.getvalue('opt_4', '0'))
res5 = int(form.getvalue('opt_5', '0'))
actual_ans_dict = {}
count = 0
b = []
for data in prsnobj.result:
actual_ans_dict[data[0]] = data[1]
if res1 == actual_ans_dict[1]:
count += 1
if res2 == actual_ans_dict[2]:
count += 1
if res3 == actual_ans_dict[3]:
count += 1
if res4 == actual_ans_dict[4]:
count += 1
if res5 == actual_ans_dict[5]:
count += 1
if count:
b.append(count)
if len(b)==0:
print "Fail"
else:
for each in b:
print "<b>Score: ", each
答案 0 :(得分:0)
假设您确实有dict
个问题需要答案,
for index, answer in enumerate(xrange(len(resp))):
if answer == ansbd[index + 1]:
print "Correct"
else:
print "Incorrect!"
以下是Enumerate的文档。如果我理解你的问题。列表resp
的插槽对应于问题编号,即dict
,ansbd
中的密钥。因此,我们遍历list
并为每个条目查看是否在ansbd
中达到了coorespond答案。
答案 1 :(得分:0)
for i in range(1, len(resp)+1): # assuming like you said, your question id's and answers are both integers
if resp[i] == ansdb[i]: # this assumes your question id's start at 1
print "Right!"
else:
print "Wrong.."
if resp = {1: 2, 2: 9, 3: 2, 4: 8, 5: 3, 6: 5}
and ansdb = {1: 2, 2: 4, 3: 6, 4: 8, 5: 3, 6: 5}
result is:
right
wrong
wrong
right
right
right
which it should be