Is there a way to add a third value to Python dictionaries?

This may be a stupid question, and I believe that there is a good chance that I take a completely wrong approach to solving my problem, so if someone knows the best way, please feel free to correct me / point me to the right.

I am trying to make a basic Python quiz program for fun, and I can’t find a good way to actually both answer questions with my answers and store the correct answers. Ideally, I would like to shuffle the answers in the question around, so for example, the answer to # 1 is not always "A", but it is too far at the moment.

In any case, I decided that the dictionary could work. I would do something like this:

test = {
    '1':    "What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #A
    '2':    "What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", #B
    '3':    "What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", #C
    '4':    "What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #D
    '5':    "What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", #C
    '6':    "What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", #B
    }

answer = raw_input(test['1'])

, . , , , - if. - ? ? ? .

+4
4

, . , , .

questions = [
    {'question':"What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",
     'answer':"#A"},
    {'question':"What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",
     'answer':"#B"},
    ...}

:

for q in questions:
    print(q['question'])
    ans = input(">> ")
    if ans == q['answer']:
        # correct!
    else:
        # wrong!

, number ( , number ):

{'number': 1,
 'question': ...,
 'answer': ...}

enumerate start

for q_num, q in enumerate(questions, start=1):
    print("{}. {}".format(q_num, q['question']))
    ...
+5

:

'1': ("What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", 'A')

test['1'][0] test['1'][1].

+5

list,

test = {
    '1':    ["What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#A' ]
    '2':    ["What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", '#B' ]
    '3':    ["What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", '#C' ]
    '4':    ["What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#D' ]
    '5':    ["What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", '#C' ]
    '6':    ["What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", '#B' ]
    }

question = raw_input(test['1'][0])
answer = raw_input(test['1'][1])
+3

python . / , , , . .

import string
import random

# a list of question/choices/answer lists
test = [
    # question       choices....                          answer
    ["What is 1+1?", "2", "11", "1", "None of the above", 0],
    ["What is 2+2?", "2", "11", "4", "None of the above", 2],
    # ... more questions ...
]

# shuffle the test so you get a different order every time
random.shuffle(test)

# an example for giving the test...
#   'enumerate' gives you a count (starting from 1 in this case) and
#   each test list in turn
for index, item in enumerate(test, 1):
    # the question is the first item in the list
    question = item[0]
    # possible answers are everything between the first and last items
    answers = item[1:-1]
    # the index to the real answer is the final item
    answer = item[-1]
    # print out the question and possible answers. 'string.uppercase' is a list
    # of "ABC...XYZ" so we can change the index of the answer to a letter
    print "%d) %s" % (index, question)
    for a_index, answer in enumerate(answers):
        print "    %s) %s" % (string.uppercase[a_index], answer)
    # prompt for an answer
    data = raw_input("Answer? ")
    # see if it matches expected
    if data == string.uppercase[answer]:
        print "Correct!"
    else:
        print "No... better luck next time"
+1

All Articles