【发布时间】:2013-12-24 18:11:49
【问题描述】:
此 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,row[1] 包含答案
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] id 是什么样的?如果他们只是简单地编号(1,2,3,4)以匹配问题编号,这将很容易
-
row[0] 只是像 1,2,3,4,5 这样的数字......
-
不喜欢这篇文章的人,请说出你不喜欢这篇文章的原因,这样提问者就会知道他的提问有什么问题。
-
@Andy 我猜是因为提问者似乎没有尝试为自己解决问题,只是要求有人去做。
-
展示你尝试过的东西是值得的,而且你被否决的可能性更小..
标签: python if-statement for-loop dictionary