Need help creating a quiz that randomly selects a question to ask

Need help creating a quiz that randomly selects a question to ask

Hey guys, so as the title suggest, I’m trying to make a basic program that asks questions and has the user input an answer. However, I want the order in which these questions are asked to be randomized. Here is my code so far:

print("Unit Circle Mastery")

cor = 0;

print("What is the sin of 0?")
ans = int(input("Answer: "))
    cor = cor + 1    
    print("Wrong. You got", cor, "answers correct. Good luck next time!")

print("What is the cos of 0?")
ans = int(input("Answer: "))
    cor = cor + 1    
    print("Wrong. You got", cor, "answers correct. Good luck next time!")

print("You got", cor, "answers correct. Thanks for playing!")

Everything is working as intended, and I plan on adding more questions. I just don’t know how I would go about randomizing the order of these questions. Thanks in advance for the help!

I think this might help you. . Half way through the page ’ Project: Generating Random Quiz Files.’ Their quiz is a simpler format for wrong answers, so you might not be able to use the entire example, but it does build out multiple tests with questions in random order and helps you build out an answer key.

Nice work, you’re on the right track. The best way to randomly select things in Python is normally the random module. Here’s the biggest issue right now, your code is a very linear script – there’s not a great way to randomly select parts of it to run. It also means you end up repeating bits of code a lot, like the answer response and exit function. Remember DRY programming practice - Don’t Repeat Yourself

Fortunately this is really easy to fix. Organize the quiz into its own data structure and write a function to administer the quiz. An easy class to use for the quiz is a List containing Tuples of question-answer pairs. As for the main function it needs to do a few things:

  1. Shuffle the quiz list of (Q,A) pairs
  2. Iterate through the list and ask each question, continue if correct, break if incorrect

Using a for loop and the enumerate() function you can keep track of the current list index in the loop, so it’s not necessary to create a counter for correct answers.

from sys import exit  # exit() is used from the shell, sys.exit() for scripts
from random import shuffle
from time import sleep

example_quiz = [
        ('What is the sin of 0?', 0),
        ('What is the cos of 0?', 1)

def administer_quiz(quiz):
    """ args: quiz (list or other random module iterable object)
        Shuffles the quiz object, then iterates through it. Each loop
        poses a question and checks the anser. Exits on incorrect answer """

    for i, q in enumerate(quiz):
        # i is the quiz list index starting at 0
        # q[0] is the question, q[1] is the answer
        print("{}) {}".format(i+1, q[0]))  # Ex: "1) What is your name?"
        answer = int(input("Answer: "))
        if answer == q[1]:
            print("Wrong. You got", i,\
                  "answer(s) correct. Good luck next time!")
    print("You got all", len(quiz), "correct. Good Job!")

if __name__ == "__main__":
    print("Unit Circle Mastery\n")
    administer_quiz(example_quiz)  # Run main function
    sleep(5)  # Delay exit for 5 sec, gives time to see final message before close

The next thing I would try to do personally is create a way for the script to read a quiz from a file rather than having the quiz hardcoded into the script.That way you can easily create and administer new quizzes without editing your Python script every time.

Good luck!

1 Like