-4

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
    else:
        print ("Incorrect Answer, the correct answer is" + answer_list(rand_num)
               break
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Emma Chisholm
  • 11
  • 1
  • 4
  • 2
    Your code is 95 percent errors, `rand_num == questions_asked`, that is performaing comparison with `rand_num` not assigned anywhere, I imagine you meant to assign `rand_num = a_random_key`, `input==answer_list subposition` would test if it worked if a reference to the `input` function is equal some variable but it does not as you ave a syntax error with the space and then some undefined variable `subposition` . You also have multiple indentation problems. You might want to start with a tutorial http://anandology.com/python-practice-book/iterators.html – Padraic Cunningham Sep 21 '16 at 00:57
  • 1
    Can you be precise what you mean by "can't make the dictionary work"? Pointing to error you are getting – Moinuddin Quadri Sep 21 '16 at 00:57
  • There are multiple issue, start with `input==answer_list subposition` – Moinuddin Quadri Sep 21 '16 at 01:00
  • Hi, sorry for any confusion. I am unsure on what code to use that allows me to call a random question and the corresponding answer from the dictionary. Thank you for the tutorial, I am self teaching and struggling to find good resources. – Emma Chisholm Sep 21 '16 at 01:01
  • `from random import choice`, `question = choice(list(questions_answers))`. You may be better of using `qs = list(questions_answers)` then `random.shuffle(qs)` and iterating over the list if you don't want dupe questions. Or `qas = list(questions_answers.items())` – Padraic Cunningham Sep 21 '16 at 01:02
  • Aside from your program itself, there are also a number of errors in your questions and answers. For example, an `if` statement allows an arbitrary number of paths, depending on the presence and number of `elif` statements ended by an `else` (if present, `else` is not required). Also, there is no such construct as an `array` in regular Python - there are lists, dicts, sets, tuples, etc., but no arrays. – MattDMo Sep 21 '16 at 01:17

1 Answers1

0

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
        else:
            print ("Incorrect Answer, the correct answer is ", rand_q_answer)
            sys.exit()


ask_questions()

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