By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

HELP - Quiz Question

P: 2
I am currently using the following code for a quiz question:

[HTML]Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[7110,7011,7101,7001]}
qa = Q1.keys()
for q in qa:
print q
ans = input('What is your answer? ')
posAnss = Q1[q]
if ans == posAnss[1]:
print "correct"
elif ans != posAnss[1]:
print "incorrect"
print " "HTML]


I want the user to be able to give their answer by using a, b, c or d.... Currently they are required to enter the actual answer e.g. 7110, 7011, 7101 or 7001.

Can anyone help me achieve this? any help would be appreciated!
Dec 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: 75
I am currently using the following code for a quiz question:

[HTML]Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[7110,7011,7101,7001]}
qa = Q1.keys()
for q in qa:
print q
ans = input('What is your answer? ')
posAnss = Q1[q]
if ans == posAnss[1]:
print "correct"
elif ans != posAnss[1]:
print "incorrect"
print " "HTML][/b]

I want the user to be able to give their answer by using a, b, c or d.... Currently they are required to enter the actual answer e.g. 7110, 7011, 7101 or 7001.

Can anyone help me achieve this? any help would be appreciated!
I'll take a wild quess,

Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[a,b,c,d]}

is it working now? :D
Dec 4 '07 #2

P: 2
I'll take a wild quess,

Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[a,b,c,d]}

is it working now? :D
It comes with the following error message:

Traceback (most recent call last):
File "\\ndrive\siu07aj\My Documents\Uni\CS Roadmap\Python project\quizEasy.py", line 1, in <module>
Q1 = Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[a,b,c,d]}
NameError: name 'a' is not defined
Dec 4 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I am currently using the following code for a quiz question:

[HTML]Q1 = {'What is 123 x 57? | a.) 7110 b.) 7011 c.) 7101 d.) 7001':[7110,7011,7101,7001]}
qa = Q1.keys()
for q in qa:
print q
ans = input('What is your answer? ')
posAnss = Q1[q]
if ans == posAnss[1]:
print "correct"
elif ans != posAnss[1]:
print "incorrect"
print " "HTML]


I want the user to be able to give their answer by using a, b, c or d.... Currently they are required to enter the actual answer e.g. 7110, 7011, 7101 or 7001.

Can anyone help me achieve this? any help would be appreciated!
Try sometiong like this:
Expand|Select|Wrap|Line Numbers
  1. v1 = 123
  2. v2 = 57
  3.  
  4. print 'What is %d x %d? | a.) 7110 b.) 7011 c.) 7101 d.) 7001' % (v1, v2)
  5.  
  6. while True:
  7.     ans = raw_input('What is your answer? (a, b, c, or d)').lower()
  8.     ansDict = {'a': 7110, 'b': 7011, 'c': 7101, 'd': 7001}
  9.     try:
  10.         result = ansDict[ans]
  11.         print "The answer you have selected is %s" % result
  12.         break
  13.     except KeyError, e:
  14.         print "You have responded incorrectly. Please enter 'a', 'b', 'c' or 'd'."
  15.  
  16. if result == v1*v2:
  17.     print "Correct"
  18. else:
  19.     print "Incorrect"
Dec 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.