Simple quiz app in Python using random.shuffle().

As a beginner Python developer, I wanted to create a simple app to practice what I have learned thus far. But I still wanted to push myself a little without overwhelming myself.

So I decided to create a simple quiz app. Everyone likes a quiz, right?

I could have quite easily hard coded a bunch of set questions and answers with a ton of Print() statements. That seemed too basic for me, and kinda boring.

For this my goal was to randomize the questions that are shown to the user / player.

There are lots of ways to randomize things in Python. And I tried several methods unsuccessfully. The way that worked may not be the best way to do this. But it works, for now. So I will be using the random.shuffle() method. If you want to know more about random methods in Python, go here.

Structuring the data

The first problem I came across was to how I was going to structure the questions list. A straight list wouldn’t work for my needs. What I needed was a way to store a correct answer as well as the question, and the list of possible answers. What I settled on, after asking on reddit for possible structures, was to nest multiple dictionaries inside of a list.

So it looks like this:

Randomizing the Questions

In order to use the random.shuffle() method, we of course need to add an import statement for random. We also need to keep a record of the correct and incorrect answers the user gives so that we can report this back to the user at the end of the quiz. So let’s define the score variable while we are at it.

I first tried pulling a random question from the questions_list with the random.choice() method. While this worked, there was nothing stopping it pulling the same questions twice during the the quiz. I know there is a way to add an exception, so that it ignores previously selected questions, but it seemed a little beyond me at the time of writing this code. So instead I called the random.shuffle() method on question_list. This shuffles the position of each nested dictionary in the list. So each time the program is run question_list[0] will change when called.

So here’s what we have so far:

Question time

Now we ask the user a question. This is simply calling the appropriate ‘key’ in the question dictionary. To print the question we add use print (q1["question"]). Next a simple for loop to list the "options".

I’m using Python 3, so I accepted the user input with the input() method. If you’re using python 2, you would use raw_input() instead.

Finally a simple if / else loop to check the answer is correct. If a correct answer is given then 1 is added to score.

Finishing up

I repeated the above code using q2 and q3 instead. I know, I know, don’t repeat yourself! I need to change that in the next iteration. Something to improve on.

All that’s left is to tell the user how well they did with print("Your final score is: %s / 3" % (score)).

Next steps

I’ve already mentioned I need to clean up this code so there’s no repetitiveness. I would also like to add in some sort of leaderboard, too. I would also like to randomize the position the answers appear. And I may even turn this into a simple web app and push to Azure for you to play with. What do you think?

What would you change? How would you do it differently? I’m keen to know.

Until next time, here’s the full script.

Leave a Reply

Your email address will not be published. Required fields are marked *