Adventures in Python: Creating a Quiz Game with Fancy Features
It’s my first Python 3 project!
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.
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.
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 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.
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.
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.
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.)