Newless_Cluebie
Newless_Cluebie

Reputation: 13

Making a quiz, how do I store the questions?

I'm trying to make a quiz game with multiple choice (4 choices). So far I've made a simple quiz containing just one question. I cannot wrap my head around a good way to index the questions.

The plan is to expand my quiz with at least 500 questions, and randomly pick a question from the question pool. How should I structure it?

This is what I've got so far in my one-question game:

def welcome():  #Introduction
    print "Welcome to the quiz!"
    print " "
    print " "


def question():  # Question function
    quest = { 'id' : 0, 'question' : "What is the capital of Belgium?" , 'a' : "Vienna" , 'b' : "Berlin" , 'c' : "Brussels" , 'd' : "Prague" , 'answer' : 'c'}
    print quest['question']
    print " "
    print "A:", quest['a'], 
    print "B:", quest['b'],
    print "C:", quest['c'],
    print "D:", quest['d']

    guess=raw_input("Your guess: ")
    if guess == quest['answer']:
        print " "
        print "Correct!!!"
    else:
        print " "
        print "Sorry, that was just plain wrong!"



welcome()
question()

Upvotes: 0

Views: 6481

Answers (6)

user7361505
user7361505

Reputation: 1

I think that quiz was made overly complicated. This code is easier too read and shorter imo.

    point = 0  
    print("The answer have to be in all small letters")
    def question(question,rightAnswer,rightAnswer2):
        global point
        answer = input(question)
        if answer == (rightAnswer):
            point = point + 1
            print("Right")
        elif answer == (rightAnswer2):
            point = point + 1
            print("Right")
        else:
            print("Wrong")
        if point == 1:
            print("You have",point,"point")        
        else:                                   # grammar
            print("You have",point,"points")    
        return

     question("What is 1+1? ","2","2") #question(<"question">,answer,otheranswer)
     question("What is the meaning of life ","42","idk")

Upvotes: 0

EvilDuck
EvilDuck

Reputation: 1

I would try importing the random function, then generating a random number between 1 and (no. of questions). Say you have 10 questions, you can type it like this;

import random
(Number) = (random.randint(1,10))

then, you add all the questions one by one, each under if statements as shown;

if (Number) == (1):
    print ("What's 1 + 1?")
    (Answer) = input()
    if (Answer) == ('2'):
        print ('Correct!')
    else:
        print ('Wrong!')

elif (Number) == (2):
    print ("What's 1 + 2?")
    (Answer) = input()
    if (Answer) == ('4'):
        print ('Correct!')
    else:
        print ('Wrong!')

And so on.

If you want to make it repeat, and ask multiple questions, start the coding with;

while (1) == (1):

And then you have a working quiz program. I hope someone found this helpful.

Upvotes: 0

idjaw
idjaw

Reputation: 26580

You can create a list-of-dictionaries that will house all this data. So you can do something like this:

quiz_data = [
    {
        "question": "What year is it",
        "choices": {"a": "2009", "b": "2016", "c": "2010"},
        "answer": "b"
    },
    {
        "question": "Another Question",
        "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
        "answer": "a"
    }
]

Then use random.choice to select a random index of your data structure.

import random

q = random.choice(quiz_data)

print(q.get('question'))
answer = input(q.get('choices')).lower()

if answer == q.get('answer'):
    print("You got it")
else:
    print("Wrong")

Upvotes: 3

gsamaras
gsamaras

Reputation: 73366

I would (based on your code):

  1. Use a list of questions. That list would be the pool of questions.
  2. I would also discard the id attribute you had, I do not see the reason of using it now.
  3. I would choose a random number in the range of 0 to the length of the list - 1, so that I can index the question pool for a question to ask the user.
  4. Finally, I would take the answer of the user, convert it in lowercase and then check if the answer is correct.

Here is the code:

#!/usr/bin/python

from random import randint

def welcome():  #Introduction
    print "Welcome to the quiz!"
    print " "
    print " "


def question():  # Question function
    question_pool = []
    question_pool.append({'question' : "What is the capital of Belgium?" , 'a' : "Vienna" , 'b' : "Berlin" , 'c' : "Brussels" , 'd' : "Prague" , 'answer' : 'c'})
    question_pool.append({'question' : "Does Stackoverflow help?" , 'a' : "Yes" , 'b' : "A lot" , 'c' : "Of course" , 'd' : "Hell yeah" , 'answer' : 'd'})
    random_idx = randint(0, len(question_pool) - 1)
    print question_pool[random_idx]['question']
    print " "
    print "A:", question_pool[random_idx]['a'], 
    print "B:", question_pool[random_idx]['b'],
    print "C:", question_pool[random_idx]['c'],
    print "D:", question_pool[random_idx]['d']

    guess=raw_input("Your guess: ")
    guess = guess.lower()
    if guess == question_pool[random_idx]['answer']:
        print " "
        print "Correct!!!"
    else:
        print " "
        print "Sorry, that was just plain wrong!"



welcome()
question()

A next step for you, would be to validate the input, for example to check that the user typed a letter, A, B, C or D.

Questions that helped:

  1. Generate random integers between 0 and 9
  2. How to convert string to lowercase in Python?

I am pretty sure Berlin is not the capital of Belgium :)

Upvotes: 0

GLHF
GLHF

Reputation: 4035

You should create a txt file and put the questions in to that file. After then you could read that file's lines and pick a line randomly(line is the question in here) with random.choice() method.

Basically, you will write your questions in a txt file. Then read the lines and print a line(question) with random.choice().

Make another txt file for answers, check that file when user answered a question.

Upvotes: 0

user5547025
user5547025

Reputation:

Store it as a JSON array

[{
    "id": 0,
    "question": "What is the capital of Belgium?",
    "a": "Vienna",
    "b": "Berlin",
    "c": "Brussels",
    "d": "Prague",
    "answer": "c"
}]

and load it using json.load.

Upvotes: 0

Related Questions