Adding special features in my Python quiz game
Last week, I was able to create my own trivia game using Python [See: How I developed my own trivia game in Python]. This time, I added 2 special features in my code to make my game more adaptable. The features I added allow me to:
- Implement CRUD (create, retrieve, update, delete) action to the questions and answers that will be used in the quiz game
- Display a certain number of players that last played the game
FEATURE 1: CRUD Action
CRUD Action Implementation
Here is a summary of how I implemented my CRUD feature. Please refer to the image above.
- Pink box: This buffers and prints the action of the program so that the user knows that his/her desired CRUD action is being implemented.
- Orange box: This shows the latest set of questions and answers.
- Yellow box: This asks what action (create, retrieve, update or delete) the user would like to do.
- Green box: If the user chooses the ‘delete’ action, this allows the user to delete a pair of question and answer.
- Light blue box: If the user chooses the ‘create’ action, this allows the user to create a new pair of question and answer.
- Purple box: If the user chooses the ‘update’ action, this allows the user to update either a question or an answer found in the latest set of answers and questions.
- Red box: If the user chooses the ‘retrieve’ action, this allows the user to retrieve the latest set of answers and questions into the local computer. Filetypes are limited to csv and txt.
- Brown box: If the user chooses the ‘quit’ action, this allows the user to exit the feature.
- Dark blue box: After finishing the action, this launches the CRUD feature again until the user chooses the ‘quit’ action.
CRUD Action Launching
To launch CRUD feature, I only needed to type ‘CRUD()’ in the IPython Console.
The latest set of questions and answers was then displayed.
The program then asked me what CRUD action I plan to do. I answered ‘create’ to create a pair of question and answer.
The program then asked me what are the question and the answer I want to add. Once they were created, the program then told me that they were successfully created.
The programed displayed the latest set of answers and questions with the pair of question and answer that I just created.
The program again asked what CRUD action I would like to do. At that time, I chose the ‘delete’ action to delete a pair of question and answer.
The program then asked what is the answer to the pair of question and answer I would like to delete. Once the pair was deleted, the program displayed that it was able to successfully delete the pair.
The programed again displayed the latest set of answers and questions without the pair of question and answer that I just deleted.
The program again asked what CRUD action I would like to do. Since one of the pairs of question and answer had wrong spellings, I chose the ‘update’ action.
The program asked me what is the answer to the pair of question and answer that I would like to update and whether I would update the question or the answer. It then asked me what I would like it to be updated into. Once the program was able to successfully update it, the program then informed me this.
The programed again displayed the latest set of answers and questions with the updated answer.
I also updated the question of the same pair.
The programed then displayed the latest set of answers and questions with the updated question.
Since I finished modifying my set of questions and answers, I chose to ‘retrieve’ it into my local computer.
I had the option to retrieve it as a txt file or a csv file. The program also allowed me to name the file. I did this twice to retrieve the set of questions and answers as a txt file and a csv file.
Shown below was the Q & A files in txt format and in csv format that were just retrieved.
Since I was able to successfully use all the actions in my CRUD feature, I finally chose quit to exit the program.
FEATURE 2: Last Game Players Display
Display Implementation
The flow of the game is almost the same as the game I initially created except for the lines that I added to display the last game players. Here is the summary of how I implemented this display feature. Please refer to the image above.
- Red box: This creates a list of the player names and a list of the player scores.
- Orange box: This asks for the name of the player and appends it to the player name list.
- Green box: This appends the score of the current player to the score list.
- Blue box: This displays a certain number of players that last played the game and their scores.
Display Launching
I asked 10 players to play the game. To launch this feature, I just typed the take() function in the IPython Console, where its argument is the number of players that last played the game that I wanted to display. Here, I took the last 10 players and their scores.
Here, I took the last 5 players that played the game and their scores.
Here, I took the last player that played the game and their scores.
The full code was uploaded in my github. Link: https://github.com/MNDC12/Trivia_Game_CRUD