Python Forum
Accessing Embedded Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing Embedded Dictionaries
#1
I am trying to make a program which tests the user on 10 math questions in random order. The questions and answers with the question number are in the dictionary mathsQs and the options for the answer is in m1 to m10. I am trying to access the embedded keys and values in the mathsQs dictionary so I can print the key and store the answer. I can also use the question number to choose which options to print. It will count the points of the person and can retry if they want at the end.

This is my code:
import random

test = True
points = 0

mathsQs = {1:{"17 * 3":"c"}, 2:{"3² * √36":"a"}, 3:{"(20% * 50)(10% * 100)":"d"}, 4:{"(5*5)* 40%":"d"}, 5:{"Is 30% of 150 bigger smaller or equal to 15% of 300?":"c"}, 6:{"17 * 6":"a"}, 7:{"5² * √400":"c"}, 8:{"(4/5)(15/28)":"b"}, 9:{"12% of 32":"d"}, 10:{"102/(34*1.5)":"d"}}

m1 = {"a":54, "b":51, "c":21, "d":49}
m2 = {"a":54, "b":51, "c":21, "d":49}
m3 = {"a":87, "b":110, "c":91, "d":100}
m4 = {"a":32, "b":10, "c":100, "d":8}
m5 = {"a":"Smaller", "b":"Equal to", "c":"Equal to"}
m6 = {"a":102, "b":98, "c":100, "d":149}
m7 = {"a":245, "b":435, "c":500, "d":600}
m8 = {"a":5/7, "b":3/7, "c":2/9, "d":1/3}
m9 = {"a":4, "b":5.55, "c":9.17, "d":3.84}
m10 = {"a":1.73, "b":1.9649122807
, "c":1.98039215686
, "d":2}


while test == True:
  print("")
  print("Welcome to the maths quiz")
  for i in range(10):
    num = random.randint(1,10)
    q, ans = list(mathsQs.get(num))
    print(q)
    print(ans)
Reply
#2
I'd like to show you a better way to do this, hope you don't mind

First, separate the quiz questions into a json file so you can create many different quizes
this program will do this (can be rewritten using GUI like tkinter as data entry program)
I also changed the dictionary into a more organized structure

SaveQuiz.py
import json
import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))

quizfilename = "MathQuiz1.json"

SaveQuiz = {
    "1": {
        "question": "17 * 3",
        "answer": "c",
        "choices": {"a":54, "b":51, "c":21, "d":49}
    },
    "2": {
        "question": "3² * √36",
        "answer": "a",
        "choices": {"a":54, "b":51, "c":21, "d":49}
    },
    "3": {
        "question": "(20% * 50)(10% * 100)",
        "answer": "d",
        "choices": {"a":87, "b":110, "c":91, "d":100}
    },
    "4": {
        "question": "(5*5)* 40%",
        "answer": "d",
        "choices": {"a":32, "b":10, "c":100, "d":8}
    },
    "5": {
        "question": "Is 30% of 150 bigger smaller or equal to 15% of 300?",
        "answer": "c",
        "choices": {"a":"Smaller", "b":"Equal to", "c":"Equal to"}
    },
    "6": {
        "question": "17 * 6",
        "answer": "a",
        "choices": {"a":102, "b":98, "c":100, "d":149}
    },
    "7": {
        "question": "5² * √400",
        "answer": "c",
        "choices": {"a":245, "b":435, "c":500, "d":600}
    },
    "8": {
        "question": "(4/5)(15/28)",
        "answer": "b",
        "choices": {"a":5/7, "b":3/7, "c":2/9, "d":1/3}
    },
    "9": {
        "question": "12% of 32",
        "answer": "d",
        "choices": {"a":4, "b":5.55, "c":9.17, "d":3.84}
    },
    "10": {
        "question": "102/(34*1.5)",
        "answer": "d",
        "choices": {"a":1.73, "b":1.9649122807, "c": 1.98039215686, "d": 2}
    }
}

with open(quizfilename, "w") as fp:
    json.dump(SaveQuiz, fp)
Then the quiz program will accept a quiz file containing any number of questions, everything is dynamic
Quiz.py
import random
import os
import json
from pathlib import Path


class Quiz:
    def __init__(self, quizname):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        homepath = Path('.')
        quizfile = homepath / quizname
        with quizfile.open() as fp:
            self.QuizData =json.load(fp)
        # print(self.QuizData)

    def get_question(self, question_number):
        return self.QuizData[question_number]['question']
    
    def get_answer(self, question_number):
        return self.QuizData[question_number]['answer']
    
    def get_choices(self, question_number):
        return self.QuizData[question_number]['choices']
    
    def test(self):
        num_questions = len(self.QuizData)
        # print(f"num_questions: {num_questions}")

        questions_asked = []
        for i in range (0, num_questions):
            questions_asked.append(0)
        # print(f"questions_asked: {questions_asked}")

        while questions_asked.count(1) != num_questions:
            num = random.randint(0, num_questions - 1)
            # print(f"num: {num}")

            if questions_asked[num]:
                continue

            question = self.get_question(str(num+1))
            print(f"\nQuestion {num+1}: {question}")

            answer = self.get_answer(str(num+1))
            print(f"Answer: {answer}")

            choices = self.get_choices(str(num+1))
            print(f"choices: {choices}")

            questions_asked[num] = 1


if __name__ == '__main__':
    quizname = "MathQuiz1.json"
    quiz = Quiz(quizname)
    quiz.test()
when run:
Output:
Question 10: 102/(34*1.5) Answer: d choices: {'a': 1.73, 'b': 1.9649122807, 'c': 1.98039215686, 'd': 2} Question 5: Is 30% of 150 bigger smaller or equal to 15% of 300? Answer: c choices: {'a': 'Smaller', 'b': 'Equal to', 'c': 'Equal to'} Question 3: (20% * 50)(10% * 100) Answer: d choices: {'a': 87, 'b': 110, 'c': 91, 'd': 100} Question 9: 12% of 32 Answer: d choices: {'a': 4, 'b': 5.55, 'c': 9.17, 'd': 3.84} Question 2: 3² * √36 Answer: a choices: {'a': 54, 'b': 51, 'c': 21, 'd': 49} Question 1: 17 * 3 Answer: c choices: {'a': 54, 'b': 51, 'c': 21, 'd': 49} Question 4: (5*5)* 40% Answer: d choices: {'a': 32, 'b': 10, 'c': 100, 'd': 8} Question 7: 5² * √400 Answer: c choices: {'a': 245, 'b': 435, 'c': 500, 'd': 600} Question 8: (4/5)(15/28) Answer: b choices: {'a': 0.7142857142857143, 'b': 0.42857142857142855, 'c': 0.2222222222222222, 'd': 0.3333333333333333} Question 6: 17 * 6 Answer: a choices: {'a': 102, 'b': 98, 'c': 100, 'd': 149}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help accessing elements of list of dictionaries Milfredo 6 2,880 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,152 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020