Not getting an expected output as compared to actual output. Although the counter here has been placed outside the loop. Here Marks are printed every after the Righ t Wrong. I want that it should get printed only once that is at the end. I have written it already outside the if loop
resp = {}
ansdb = {}
counter = 0
for i in range(1, 10):
resp_i = form.getvalue('opt_%d' %i, '0')
resp[i] = int(resp_i)
print "<br>"
for row in prsnobj.result:
ansdb[int(row[0])] = int(row[1])
print "<br>"
for i in range(1, len(resp)+1):
if resp[i] == ansdb[i]:
print "<br>Right"
counter += 1
else:
print "<br>Wrong"
print "Marks:", counter
Actual Output:
Right Marks: 1
Right Marks: 2
Wrong Marks: 2
Right Marks: 3
Right Marks: 4
Expected:
Right
Right
Wrong
Right
Right
Marks: 4
The last line:
print "Marks:", counter
is inside the for loop, so just correct it, and it should work:
resp = {}
ansdb = {}
counter = 0
for i in range(1, 10):
resp_i = form.getvalue('opt_%d' %i, '0')
resp[i] = int(resp_i)
print "<br>"
for row in prsnobj.result:
ansdb[int(row[0])] = int(row[1])
print "<br>"
for i in range(1, len(resp)+1):
if resp[i] == ansdb[i]:
print "<br>Right"
counter += 1
else:
print "<br>Wrong"
print "Marks:", counter
将此行移出循环范围。
print "Marks:", counter
Write the last print statement outside the for loop. Something like this:
resp = {}
ansdb = {}
counter = 0
for i in range(1, 10):
resp_i = form.getvalue('opt_%d' %i, '0')
resp[i] = int(resp_i)
print "<br>"
for row in prsnobj.result:
ansdb[int(row[0])] = int(row[1])
print "<br>"
for i in range(1, len(resp)+1):
if resp[i] == ansdb[i]:
print "<br>Right"
counter += 1
else:
print "<br>Wrong"
print "Marks:", counter
I tested this and it works correctly:
resp = {}
ansdb = {}
counter = 0
for i in range(1, 10):
resp_i = form.getvalue('opt_%d' %i, '0')
resp[i] = int(resp_i)
print "<br>"
for row in prsnobj.result:
ansdb[int(row[0])] = int(row[1])
print "<br>"
for i in range(1, len(resp)+1):
if resp[i] == ansdb[i]:
print "<br>Right"
counter += 1
else:
print "<br>Wrong"
print "Marks:", counter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.