Adventures in Python: Creating a Quiz Game with Fancy Features

Roch Derilo
Analytics Vidhya
Published in
4 min readOct 9, 2020

It’s my first Python 3 project!

A photo of two people hiking on a mountain.
Hiking the slopes of learning Python may be hard at first, but it’s worth taking every step!

Just like hiking a mountain for the first time, creating your first Python program can seem like a difficult uphill. There’s this feeling of uncertainty as to whether your current knowledge and skills are enough to be able to complete it. There may also be this doubt if you can even start making one!

As a budding Pythonista, I knew that I have to take the first few steps. I did. And it’s not without difficulties. In the end, I was able to climb a bit and I am starting to enjoy the view!

Creating the Basic Quiz

I am also into astronomy so I decided to write a quiz about planets in the Solar System. I called the game the “Solar System Quiz 2020”. I wrote the basic quiz on a single .py file using Spyder.

Screenshot of the first part of the quiz script I wrote in Python.
I set the variables name as an empty string and score as zero.

I used a dictionary such that the questions correspond to the answers as keys-value pairs. I also applied a for loop to parse through the dictionary and print each key as a question.

To get the player’s name as an input, to count the score, and to display whether the player passed or failed the quiz — I applied some conditional statements. I used 70% as the passing rate.

Screenshot of how the quiz script will show the score in the console.
This is how the score shows up in the console. I got a perfect score, haha.

Feature #1: Tracking the Top Scorers

Being able to complete the basic quiz and having it running as expected has built up my confidence. I then proceeded with adding a couple more features to the game.

I created another file that will call the original quiz file. I call this the “game file”. Upon realizing that I may need to make significant changes to the quiz file, I decided to back it up and save it as another .py file. It feels awesome that I’m not only writing my second Python script but also making these scripts interact with each other.

A screenshot of the modified quiz script I wrote in Python.
I modified the original quiz script so I could use the same variables and call the quiz function.

A couple of changes that I made to the original quiz file were: I defined the function play_game() to run the quiz; I also defined some local and global variables that can be used in both scripts.

To keep track of the players and their scores, I created an empty dictionary which will be filled up as more players keep on taking the quiz. In the end, I used the sorted() and len() functions to be able to display the top ten scorers.

Feature #2: View, Add, Edit, and Delete Questions

Taking things further is the application of CRUD in Python. Just like in my game script, I created another .py file to be able to create, read, update, and delete entries on a separate .csv file. These entries are the pairs of questions and answers, separated by a comma.

The original ten questions were the first ten items pre-written into the .csv file. Most of the script is a while loop with several conditionals to be able to execute any of the CRUD actions.

A screenshot of the question manager script I wrote in Python.
My own take on “doing the CRUD using Python”.

Based on the option that the user uses, a different conditional will be run, which displays its own unique message and run the requested actions involving the external question file.

A screenshot of how my question manager script runs in console.
I decided to make it more fun by involving the user in the CRUD process.

I used some strategic messages to prompt the user to type the questions in the format I wanted. I also realized that displaying some strings of messages can inform the user of what is going on as they are running the program.

A screenshot of how my Python code runs in a console.
This is how it looks like when I ask the user to add a pair of question and answer.

Taking this whole adventure of learning Python to the next level! Next, we’ll explore Python packages which can be helpful in my pursuit of data science, such as NumPy, pandas, and Matplotlib.

Check out my scripts on GitHub. Tune in for my next adventures in Python!

(This is the second article in my series. Check out the first.)

--

--

Roch Derilo
Analytics Vidhya

A lover of data, tech, and hot choco. Supports anything open source and its power in the data value chain.