Emma Chisholm Emma Chisholm - 1 year ago 55
Python Question

How do I create a randomised quiz using a dictionary?

I have been attempting to make a randomised quiz but can't make the dictionary work, please help! This is my code so far.

Import random

questions_answers = {"Which is the comparison operator for Not Equal to? Enter the number of the correct answer, 1. = 2. == 3. != ":"3",
"How many paths through a program does an IF statement allow? 1. One path 2. Two paths 3. Three paths":"2",
"What is this curly bracket called { } ? ": "Braces",
"What does the statement 'print' do? 1. Output a hard copy of a program to a printer 2. Output a message on the screen 3. Print a hard copy of a flowchart to a printer":"2.",
"How many statements are there in this line of code: print('If I am 17, I can drive a car')? 1. There are two statements - 'print' and 'if' 2. There are no statements 3. There is one statement - 'print'":"1",
"What is a variable? 1. A variable is a number 2. A variable is a message input by the user 3. A variable is a location in memory that we use to store data": "3",
"In programming what name is given to the data type which represents decimal numbers?": "Float",
"In programming, what is iteration? 1. The repetition of steps within a program 2. The order in which instructions are carried out 3. A decision point in a program": "1",
"What is the name given to a loop that continues indefinitely? 1. An infinite loop 2. A forever loop 3.A continuous loop": "1",
"What values does a Boolean expression have? 1. Yes or No 2.True or False 3.Right or Wrong": "2",
"How many elements would the array 'student_marks(10)' hold? ": "10",
"In the array 'colours('Purple', 'Blue', 'Red', 'Green', 'Yellow')', what is the value of the element colours(3)?": "Green",
"What is the difference between an array and a list? 1. An array holds numbers, whereas a list holds strings 2. An array can only hold data of the same data type, whereas lists can hold values of different data types 3.An array holds both numbers and strings, whereas a list only holds numbers": "2",
"Why are functions used? 1. To make code more readable 2. To reduce repetition of code and to calculate a value 3. To make decisions": "2",
"What is a bug? 1. An error in a program 2. An error in an algorithm 3. A way of halting a program that is running": "1",
"What is a syntax error? 1. A mistake in the logic of the program 2. A spelling or grammatical mistake in the program 3. A way of halting a program that is running": "2",
"Which of the following contains a syntax error? 1. print('We love computing') 2. print('We love computing') 3. print(answer)": "2",
"Why are comments included in code? 1. So that the program's title is known 2. To make it clear who wrote the program 3. To help programmers understand how the program works": "3",
"If a syntax error is present, what will happen when the error is encountered? 1. The program will crash when the error is encountered 2. The program will behave unexpectedly 3. The program will not start": "1",
"What is an array? 1. A list of variables 2. A list of strings 3. A series of memory locations, each with the same name, that hold related data": "3"}

def ask_questions():
print ("You may quit this game at any time by pressing the letter Q")
while True:
rand_num == questions_asked
if input==answer_list subposition:
print ("Correct!")
score +=1
print ("Incorrect Answer, the correct answer is" + answer_list(rand_num)

Answer Source

Ok, I decided to do this because my python is a little rusty and I thought the question was interesting.

import sys
import random

def ask_questions():
    score = 0
    print ("You may quit this game at any time by pressing the letter Q")
    while True:
        rand_q = random.choice(questions_answers.keys())

        rand_q_answer = int(questions_answers[rand_q])

        user_input = input("Your answer? ")

        if user_input == rand_q_answer:
            print ("Correct!")
            score +=1
            print ("Incorrect Answer, the correct answer is ", rand_q_answer)


This code seems to work fine, although I'm sure it can be improved.