To be a data scientist, one must learn programming.
To aid in our data science journey, we were taught one of the most popular and easy to learn high-level programming language: Python.
Upon doing the exercises and assignments for our data science class, I learned to appreciate Python. There are a lot of functions, libraries and packages included that will save you time and effort in writing your codes, unlike in C which based on my experience, most often than not, will need you to build everything from scratch.
For our Python homework, we were tasked to create a quiz about anything under the sun with the following features:
- 10 Question Quiz
- Prints name of Quiz Taker and score with a pass/fail remark
- Able to print past 10 scores of previous quiz-takers
- Apply Create, Update, Read, Delete (CRUD) Operations
CREATING THE QUIZ
- QUESTIONS AND CHOICES
The Python quiz was given as an exercise on the first day we were taught Python. We were given an hour to create our simple quiz with only the basic feature of a user answering the quiz and printing the name and score after. Usually, given a new activity with limited time pressure, I always panic deep inside while trying to do it. During that time, my mind goes finds a solution albeit sequentially or in a step-by-step manner. This resulted to having the program hard-coded.
- Version 1
Version 1 of my questions and choices included functions for each question and an input prompt and several if statements to check the entered answer either in upper case or lower case while incrementing the score if correct. It worked but yeah, it was a lot of lines of codes and I know there are more efficient ways to do this, but my mind was in panic mode so this was the best that I could do that time.
- Version 2
After some of our batchmates discussed their codes and when our instructor mentioned and taught new knowledge and concepts in Python, most especially, the use of dictionaries, I’ve seen its usefulness and tried it to improve my quiz program. From the several if statements to check the answer, the code was shortened to 5 lines by putting the questions and answers in a dictionary and using only a for loop and if statement to show the questions and choices, get and check the answers and record the cumulative points.
- Version 3
2. FLOW OF THE GAME
Initial versions of the quiz only take an input prompt of the name of the user:
While we were tasked to include the CRUD operations, I decided to think and plan first on how the flow of the quiz goes before coding. It is always useful to visualize first before writing your codes the overview of how your program will run. Through this, it is easier to develop the program because you have an outline. Directly coding on what comes into your mind without a concrete plan may lead you to several obstacles you didn’t expect.
Upon starting the game, the user will be shown several choices on what he/she wants to do, including answering the quiz, adding a question, deleting a question, updating a question or quitting the quiz.
I put this part of the program in a if __name__ == “__main__” block to set it as the main program as I created several functions for different purposes. To know and understand more on why it is useful to use this block, you may refer to this Medium article. Through a while loop and several if-elif statements, a user is prompted to input the number of what he/she wants to do. Picking a number will direct the program to the function specific to its use.
3. ANSWER QUIZ
Upon choosing to answer the quiz, a series of questions will show which the user should answer. Answers can be upper-case or lower-case letters between three choices, A, B or C. Since questions, choices and correct answers are loaded in the json file of quiz questions, it is important to read the json file to access the data and continue with the quiz.
Basic file handling operations are done through this argument:
file = open("filename","mode")
where mode are r for reading, w for writing, a for appending, r+ for reading and writing. Once a files is open, it is important to close it after use to close it completely and terminate resources in use. I’ve found an efficient way where opening and closing files are done using one code and this is using the command
with open(“filename”,"mode") as file:
Here’s the basic code in handling file operations and loading the data in a specific variable.
As the quiz is answered by the user, the points are recorded per each correct answer. At the end of the quiz, the results of how well the user performed is shown together with the Top 10 Leaderboard:
To monitor the users or takers and their scores, for each game played, the user name and score is recorded in a text file with a semi-colon (;) separator to distinguish between the two variables.
At the end of each quiz, the userscore text file is read and at the same time, the current user and his/her score is being recorded. The highest 10 scores are recorded in decreasing order and printed at the end of the game.
4. CRUD Operation
The CRUD Operations are done through the Add Question, Delete Question and Update Question option.
Adding and updating a question will prompt the user to enter the new question, the choices and the correct answer. For updating a question, the list of available questions are shown first for the user to choose what to update.
The new question, choices and answer are then stored on the json file for next use. The most crucial part in the code where it took me a couple of hours to make the code work is on a difference of 2 lines of codes.
I thought json.dump will already do the work but the seek() and truncate() commands were essential to add and update the questions, choices and answers in a way that still follows the key-value format in the json file. With this, I learned that even the smallest of details on the code are important to make it work and it may take you several hours to figure it out so it is essential to take a break sometimes ;)
Lastly, here’s the program when deleting a question is chosen:
The list of questions is also shown first for the user to pick the number of the question he/she wants to delete. As you can see on the next answer quiz program, the new first question is the 2nd question from before since the user deleted the previous first question.
The del command and file write command were helpful for me to be able to make the delete function work.
Overall, the quiz exercise was helpful in studying Python and improving our Python programming skills. I’ve learned new functions, several syntax on how some data can be printed and accessed and expanded my knowledge on coding in Python.
My latest quiz is not yet perfect since there are some lines of codes that I know I can improve on by writing shorter and more efficient ones. There are also some error handling operations that are not yet incorporated especially when entering the answers. I’ve also wanted to add more features such as including the time and date the quiz was taken, how long it took for the user to finish the quiz and overwriting the scoreboard list if the user has answered several times. I’ll try to update it next time but for now, this would do. You may play the quiz or check my program through this Github repository.
UP NEXT: Exploratory Data Analysis using Pandas and Matplotlib (Github repository on pandas and matplotlib exercise)