Fake and True

Keith Monreal
4 min readOct 4, 2020

--

A Simple True or False Python Quiz with CRUD functions Using a CSV File

I have two objectives for this task.

  1. 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
  2. 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().

  1. addquestions() — to add questions
  2. delquestions() — to delete questions
  3. play() — to play the game
  4. 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:

  1. calls a question
  2. saves user’s answer in a list
  3. calculates the score with a counter “score”
  4. 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

--

--

Keith Monreal
Keith Monreal

Written by Keith Monreal

A statistician, aspiring to be a unicorn

No responses yet