简体   繁体   中英

Is there a way to attach a 3rd value in Python dictionaries?

This may be a stupid question, and I believe there is a good chance I am taking the completely wrong approach to solving my problem, so if anyone knows a better way, please feel free to correct me / point me in the right direction.

I'm trying to make a basic quiz program in Python for fun and I can't seem to find a good way to actually both call the questions with their answers AND store what the correct answer should be. Ideally I'd like to shuffle the answers within the question around as well so for example the answer to #1 is not always "A" as well, but that's going a step too far at the moment.

Anyway, I figured a dictionary could work. I'd 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'])

Which I could use to easily print out questions either in order or randomly with a little modification. However, I have no way of actually attaching the correct answer to the dictionary which means I'd have to do something like make ridiculous amounts of if statements. Does anyone know a solution? Is there a way to add a 3rd value to each dictionary entry? A work-around I'm completely overlooking? Any help would be greatly appreciated.

Make the dictionary's values into tuples:

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

You can then access the question through test['1'][0] and the answer through test['1'][1] .

It looks like you're laying out your data structure wrong. Instead of a dictionary of tuples or lists as your other answers are suggesting, you SHOULD be using a list of dictionaries.

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"},
    ...}

This will let you iterate through your questions with:

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

If you still need numbers, you could either save number as another key of the dictionary (making this kind of like rows in a database, with number being the primary key):

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

Or just iterate through your questions using enumerate with the start keyword argument

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

Put the value into a list as in

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' ]
    }

Then access it as

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

Ditch the dictionary and let python do more of the work for you. You can represent each question/answer group with a list where the first item is the question, the last item is the answer and all of the items in between are multiple choice answers. Put those in another list and its easy to shuffle them up.

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"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM