How I developed my own trivia game in Python

Mary dela Cruz
4 min readSep 22, 2020

It has only been a few days since my rusty Python skills were put to test by the Rock, Paper, Scissors game I made using Python. If you haven’t read my story about it, check it out and then try to develop your own! To continue solidifying my Python fundamentals, I decided to develop a super easy game of trivia. The game mechanics is pretty straightforward. The player just has to correctly answer the questions I inputted in my code but don’t worry! As I already said before, the questions are super easy! The hardest (and the most fun) part was already handled by me, which was to make my code as flexible as possible towards changes. How flexible, you might ask. Well, continue reading to find out.

Time to code!

The Python IDE that I used is Spyder. To create the game, I just imported the choice function from the random module and then I just created the following dictionaries and functions:

  • quest_ans dictionary: This consists of several pairs of the question and its correct answer.
  • exclaim dictionary: This consists of a list of statements that my code can say for every correct answer or every wrong answer.
  • check(ask) function: This checks if the player’s answer is part of the predetermined choices. For this case, the predetermined choices are ‘Yes’ and ‘No’. If the answer inputted is neither ‘Yes’ nor ‘No’, the question, which is the ask argument, will be asked again until the answer of the player finally becomes either ‘Yes’ or ‘No’.
  • game(Quest_Ans=quest_ans, Exclaim=exclaim, score = 0) function: This takes in the quest_ans dictionary, exclaim dictionary, and the initial score, which is 0, as arguments. They are to be used for the actual game process.

The code for the functions are shown below:

The flow of the game() function is as follows:

  1. Display introductory remarks.
  2. Ask if the player is ready to play or not. The answer of the player is limited to ‘Yes’ or ‘No’. To implement the limitation, use the check() function. If the player answered ‘Yes’, then start the game but if the player answered ‘No’, then end the game.
  3. Suppose that the player is ready. Take the 1st pair of question and answer in the quest_ans dictionary and then ask the question. If the answer of the player is the same as the correct answer, then randomly display one of the statements in the exclaim dictionary that announces that the player is correct. A point is also added in the score. If the answer of the player is different from the correct answer, then randomly display one of the statements in the exclaim dictionary that announces that the player is wrong.
  4. Repeat step 3 until all the pairs of question and answer in the quest_ans dictionary are executed.
  5. Display the score of the player. If the player’s score is less than or equal to half the total number of questions, then the player fails. If not and the player answered all questions perfectly, then declare that the player got a perfect score. If this also false, then tell the player that he/she did a good job because he/she was able to get a passing score.
  6. Ask if the player wants to play again or not. Like in step 2, the answer of the player is limited to ‘Yes’ or ‘No’ so use the check() function. If the player answered ‘Yes’, then repeat the previous steps but if the player answered ‘No’, then end the game.

Notice that my code can adjust according to the dictionary so as the developer, I can easily just add/remove/modify questions and answers in the quest_ans dictionary, and statements in the exclaim dictionary. This is the extent of the flexibility of my code given the current level of my Python skills. If you have any suggestions on how to improve this, please comment below!

Time to play!

Of course, I already know the answers to all the questions so instead of answering my own questions, I asked my little brothers to play the trivia game. One of them got a perfect score while the other one got 2 mistakes. As expected, both of them had fun playing it. My brother who made mistakes even wanted to redo the trivia game so that he can get a perfect score. Basically, the hook of the game is its easiness and simplicity. You can play it without any worries! I was also satisfied with how it turned out because the game worked without any issues.

If you just want to view the full code right now, just click on the Google Colab link below. Note that this is for viewing only.

Google Colab: https://colab.research.google.com/drive/1bJiKcgQakTtXrYLRNHu2m3KmPXcUvtUN?usp=sharing

But if you want to play the game, click on the Github repository link containing the Trivia Game code. There you will see a notebook document (.ipynb file) and a Python script file (.py file). You can either open it in Google Colab as a notebook document, or create a local copy of the repository and then open the file using the IDE you want to use. Once you launched the file using your desired IDE, type game() in a code cell to start the game.

Github Link:
https://github.com/MNDC12/Trivia_Game

Make sure not to peep at the answers when you are playing or else, you will lose your fun. Happy playing and happy coding!

--

--