Skip to content Skip to sidebar Skip to footer

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 fr

Solution 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:

forq 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']))
    ...

Solution 2:

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])

Solution 3:

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 turnforindex, 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 letterprint"%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 expectedif data == string.uppercase[answer]:
        print"Correct!"else:
        print"No... better luck next time"

Post a Comment for "Is There A Way To Attach A 3rd Value In Python Dictionaries?"