Fake and True
A Simple True or False Python Quiz with CRUD functions Using a CSV File
I have two objectives for this task.
- QUIZ: To create a quiz which tells the player if he passed or failed in the test and enables user to print the last 10 users with their respective scores
- CRUD: To Create a csv file with the list of questions and their respective correct answers, Read and Update the csv file if the questions are added or Deleted
INITIALIZE
Before I discuss the quiz proper, I initialized variables that I will be using in the loop. I saved 5 questions in questions which is a list and their respective answers in right_answers.
I need to make a dictionary from these lists and save it a csv file. Because I will be updating this dictionary and the csv file as part of my CRUD objective, I defined a qanda_dict() function for making a dictionary and a qanda_csv() for saving the dictionary into a csv file. I then loaded these functions so we have an initial dictionary and csv file if the player wants to access the quiz. This is where the Create and Read in CRUD comes in.
open(‘qanda.csv’) creates a csv file of the qanda dictionary. Since I need to change the column names, I need to read this csv file with pd.read_csv function and set the column names which are “Question” and “Answer”.
After I initialized these objects, I then created functions which was used in a home() function which is the quiz proper.
MAIN MENU
In the screenshot below, we can see 4 functions inside the main quiz function called home().
- addquestions() — to add questions
- delquestions() — to delete questions
- play() — to play the game
- records() — to print last 10 players and their scores
1. ADD QUESTIONS
For the addquestions() function, I let the user input the question, the correct answer and used append to update the questions and answers lists. I once called the function for saving it in a dictionary and csv, qanda_dict() and qanda_csv().
The addanother() function lets the user add another question. If the user wants to add another function, addquestions() will be called again. If not, there will be an option to go to the main menu: home().
2. DELETE QUESTIONS
The logic for deleting questions is similar with adding questions. Instead of append, I used pop to delete the question the user wants to delete. I also called qanda_dict() and qanda_csv() function to delete the questions in the csv file.
3. PLAY
I used a while loop for the questionnaire that does these for each loop:
- calls a question
- saves user’s answer in a list
- calculates the score with a counter “score”
- assesses if it’s already the last question
If the 4th logical question is true, that is, it is already the last question, lastquestion() will be called. This function will tell if the user passed or failed and store his name and score in the names and scores lists. These lists will be used in the next function.
4. RECORD
In this function, the last 10 players and their scores will be printer given that there are at least 10 players that finished the quiz.
That’s it!
It looks easy but as a beginner in Python, I know I can make my code more efficient and shorter as I learn more functions and libraries.
You can play the quiz here: https://github.com/keithmonreal/FTW/blob/master/CRUD.ipynb