Let’s Keep a Record of Your Score

Queenie Pamatian
The Startup
Published in
5 min readSep 29, 2020

For the day 5 of our data science training, we continued learning python. We picked up from where we left off and on set to new knowledge. This session is much more challenging because the pacing of the lessons is a whole lot faster.

For this week’s task, we were asked to recalibrate our quiz that we made the Saturday before. What is expected to this new version of quiz is to include CRUD techniques (create, read, update, delete).

As a refresher, let me show you the original quiz I made:

Quiz

So as you can see at the top part, I declared the variables that I will be using throughout the code. Afterwards, I declared the functions.

My functions are:

  • intro(name) — this is called at the start of the program wherein a user is asked to input his/her name.
  • rating(score) — this function will compute the ratings of the users. It will assess if the user needs to study more or it already passed.
  • quiz(question,answers) — this is the main part where the questions are being loaded and the user input answers are being compared to the answer key. I used a separate list for questions and answers and just utilized an iterator to match the indices of respective questions and answers. A better way to do it would be through dictionary.

Here comes the modifying part.

I decided to add another function called loadscores. In this function, my goal is to write the names, scores, and the ratings of the user (recently added to say if a user failed or passed) in a text file.

I made 5 version of the quizzes and opened more than 10 tabs of google chrome just to arrive at this:

loadscores()

since I used a list as a place holder for the names, scores, and ratings of the users, I can use the for loop to iterate through the user details.

At the beginning of the code I imported from os import path so that I can have a way to check if the text file exists. This is important because this will determine if i should write the text file or append it (thank you google!)

After determining whether I should append the text file or create a new one, I coded the lines for the content of every column of the text file (name, score, rating).

Let me modify first the code so that the square brackets wont be included when writing using .replace()

using replace()

Let’s check out the file:

first user: Queenie
scoreboard.txt

Works well!

Next is the tricky part. Deleting the first entry whenever the scoreboard reaches 10 rows. For a simpler testing I just used 3 as the limit so that it will be faster to run the test.

code for delete and update

So in here I created a variable called last_list taht reads the existing scoreboard.txt. The for loop will count the number of lines in the file and placed to a variable called numscores.

The if loop is the condition that sets when to delete an item. So as I’ve said I will set it to 3, and if it reaches 3, the first row in the text file will be deleted.

I created a variable called newscore as a holder for rewriting the new lines in the file. Prior to deleting, there are 4 lines already in the txt, and now since we deleted the first index, we are expecting t write 3 lines in our text file.

Let’s demonstrate if its correct

Remember that on our first run we already have 1 item in the text file. the record of user Queenie. So we just have to run 2 more times.

scoreboard.txt

Now user Dylan and Kier were able to take the quiz. After running the code again for another user, we are expecting the record for Queenie to be deleted.

scoreboard.txt

Our del works! :D

Extra: Converting it to a csv file.

If we want to study the data that was stored into the text file, It is best to have it in a CSV form. So I added extra codes (in a separate py) to convert my txt file into a csv with 3 columns.

dataviz,py

Just a few lines of code but very powerful :)

Here’s the result:

scoreboard.csv

Hello again! Apparently I misunderstood the instruction so I need to have modifications.

So task 1 should be able to display the 10 most recent game plays in the terminal, rather than writing a file for it.

Display score

It is pretty much the same logic with writing it in file, but this time I just have to print the scores. I still kept the write to file function though.

The second task is to be able to apply CRUD on the questions (at first I applied it on the scores part).

For that, I made an admin mode where I can have the options to delete and add questions.

Add questions
Delete Questions

I just patterned my codes with the previously made function where I write the scores in the text file and access the lists I made.

I have reuploaded it under the same repo but do checkout https://github.com/ftwqueenie/quizwithCRUD.

--

--