Build A Quiz With Python
I learned a lot in this course
12 min read
Read TimeMay 23, 2024
Published OnI just finished the Python Bootcamp on Udemy, and it took a little longer than I had planned, but faster than the program planned. All in all, I’m well on schedule. I personally found this course the most interesting one so far.
The course itself has 2 milestone projects and 1 capstone project, and for the capstone project the course actually suggested working on an own project. I didn’t know that, and in that sense, I may have been ahead of the course, because I already had the idea to work on an additional project outside of the course, some lectures before the capstone project.
So I reached out to my coding buddy, Daniel, who’s also doing the traineeship. I told him about the idea to create a quiz game in Python where you have to guess the nationalities of our co-workers, or if you know, then you know. But this idea came to be, because I recently received an email that there will be cultural week at the office, and apparently there are a total of 33 different nationalities here at Devoteam in the Netherlands.
Diversity, check.
Anyways, I thought it would be interesting to see how well everyone at the office knows one another. But another benefit of working on a project like this, in addition to showcasing our Python skills, is to showcase or improve our “interne zichtbaarheid”, as the newest members in the team. Daniel was onboard with the idea, but he gave a really good suggestion, which is to show a fun fact about a person, and then to guess the name of the person. I liked his idea. It’s a bit more diverse while not emphasizing on diversity itself.
So we collected some fun facts, and initially I wanted to just send out a Google Form to everyone with the fields “full name” and “fun fact”, and just start writing code, but Daniel reminded me that we won’t be able to increase our “interne zichtbaarheid” doing it this way, which is a valid point, so instead, we started to collect these fun facts “personally” a.k.a. via Google Chat, which in 2024 is as personal as it gets.
And yes, I may have used a written template that I may have copy/pasted and personally distributed among our co-workers explaining our objective, but it’s a quite long list of people, and I do need to stay time-efficient.
And now the fun part starts, which is the writing of Python code. Not that it’s not fun socializing with co-workers, but as a natural born introvert, I do prefer PyCharm/VSCode over Google Chat. Just kidding! I haven’t always been an introvert, I mean, I prefer socializing.
But let me explain the end goal first.
What I want to achieve is a website where you’ll have to write your full name, no password or registration, just your full name, then a quiz with 10 random fun facts where the user needs to guess the person to whom that fun fact “belongs” to, and finally a leaderboard with the name and score of that user.
I have zero experience with deploying a Python app as a website, and this was also not in the Python course, so I think that this will be the more challenging part.
For the questions themselves, I’m thinking of inputting that in a CSV file and read from that file, since reading from a CSV was covered in the course, and I just also happens to think that it wouldn’t be great to hard code all these values in the program itself, but this is just my assumption.
Regardless, I do think it’s just easier to insert and update data that follows a similar structure in a large quantity, like in a spreadsheet. Plus it’s a nice way to demonstrate the use of:
The logic part for the quiz itself doesn’t seem that complicated. I’m thinking of creating a list of dictionaries to store all the data, and I think I want to separate that into questions, options, and answers. So each question/fun fact has four different options, which will be multiple choice options (a, b, c, or d) and 1 answer. Since the options have multiple values, we can use a nested list. So the format would look something like this:
I apologize to my vegan readers, but that’s just not an option. I’m kidding, relax.
So our goal is to have at least 30 fun facts, but at the same time we don’t want to give our users the feeling as if they’re doing one of those Azure exams, and we think that 10 questions is just the right amount of questions to ask. But we also don’t want to keep asking the exact same 10 questions, duh, so …
Yes, we will use random.shuffle() to shuffle the order in the list, and then we need to take 10 questions from that list, or assign 10 questions from that list, which we can do with [:10], which will take the elements from index 0 up until 9, which is 10 elements.
We also need to set a score variable to keep track of the score, which initially will be 0.
Alright, I think that’s all the setup we need.
Now we need a couple of print statements telling our users what this quiz is about, how it works, and also prompt for the name of the user, but perhaps for now, let’s just focus on how to get those 10 random questions printed, ask for the user input, check if it’s correct or incorrect, continue to the next question, and print either correct or incorrect based on the user input, and calculate the total score.
We know we will need a for loop to loop through the list of dictionaries and print the 10 questions. But we also need to print the options belonging to those questions, which we know are in a list, thus we need to create another for loop to loop through that list, because we want to display all four options, meaning we need a nested for loop, because remember, the list is also nested within the list.
Next we need to ask the user for input and do a couple of validation checks, and we also need to do this in a while loop, to continue asking the user for the correct input, not answer, but input.
But before that, let’s control whatever we can control, so in our case we want to convert the input to lowercase with the .lower(), so we don’t have to worry if the user enters the answer in uppercase, and we need to remove all the whitespace with the .strip(). Then we need to make sure that the user input must be either a, b, c, or d, so we actually need to create a “benchmark” or actually a list with the accepted answers, and check if the user input is in this list.
While True will always return True, so it’s essentially an infinite loop, meaning we need to break out of this loop, if the user input matches our validity check. Else, keep asking for the correct input!
I don’t know why I put an exclamation mark at the end, that was so unnecessary. Anyways.
We also want to display correct if correct or incorrect with the correct answer if incorrect, and update the score, which by now we know is score += 1. Speaking of displaying the correct answer. Since I only want to show the correct name, we do need to do some string manipulation, or rather list manipulation.
First, we need to loop through all the options and check if the first character for every option starts with the character that is the value of the key answer. If that's the case, then assign that value to correct_name, but before that, split the closing parenthese and the space, meaning remove that from the list and return the value at index 1, which should be the first and lastname, because we've split from the middle. The output option would be something like ['a','John Doe'], because remember, .split() returns the strings as a list, so selecting index 1 returns 'John Doe'.
Finally, we need to calculate and display the score.
Well, not finally, because finally finally, we want to prompt the user for their name, store their name and score in the database with SQLite, and display it on the leaderboard. But since the next topic in the DevOps track is about SQL, I will save this task for later.
Btw, I know that I haven't actually imported the csv file, but I will also add that together once I've finished the SQL course.
Bye for now.