Drew Womble
Posted on June 9, 2023
This will be a simple tutorial on how to create a quiz game with Structured Query Language aka SQL and python. If you're unfamiliar with SQL check it out here. If you want to follow along you can use this repository I created as a template. First off we will want to get started with the Quiz
class:
We'll want to go ahead and import sqlite3 and set some variables so that we can connect to our quiz.db
file. Create the __init__
method, getters, and setters for the question and answer attributes. Then we'll want to build a class method for our create_table
, drop_table
, create
, get_all
, and find_by_question
. We'll also create our save
method:
Now we can work on the cli.py
file:
Here we created a main
method to allow us to loop through our menu and when a user input's and option it will take us to the corresponding helper functions that we are about to create. Note the .strip()
at the end of line 13. That will allow us to remove and spaces the user might have accidentally entered giving us our desired response. The import at the top is preemptively imported so we can test the cli as we go. If you wish to test the cli at any point you can run the seed.py
file to fill the database with dummy data. Then run the cli.py
file to actually see what's happening with the cli. Now we just have to build our helper functions and we're done! We'll start with the welcome()
and menu()
methods:
We'll go ahead and import random and sqlite3, and we'll create the sqlite variables that we created in the Quiz
class, we will need them later. These functions are pretty simple allowing the user to actually see what they should input to get to their desired destination. Now we'll want to work on the ask_question()
function:
Here we created another helper function for our helper function randomize_questions
. This function selects the questions from our database and gives us a single random question with its corresponding answer. The ask_question()
function takes the question and answer given to us by randomize_questions()
and compares the answer to the user's input. If correct the terminal will say you got the answer right and if not it'll tell you you're wrong and give you the correct answer. After answering the question right or wrong you'll get sent to another helper function for our helper functions to determine if the user wants to keep answering questions or if they want to go back to the menu:
If the user chooses yes they'll be spit back to ask_question()
if they choose no they'll be redirected to the menu. Now we will want to create our function to handle creating new questions:
Here we give the user option to input the question and answer they want to add to the database and store them as variables. Then we can use the create
method from the Quiz
class to actually add the users inputs to the database. Lastly we can add validation by using the find_by_question
method to check that the users inputs were successfully added to the database. Now we just need to add functions to handle the user exiting the cli and for invalid user inputs:
The exit_cli()
function allows the user to exit the cli in a user friendly way. Although ctrl + d works too. The invalid_inputs
function simply prints a message to the user telling them their input was invalid and returning them to the menu. Note you don't have to call menu()
in invalid_inputs()
because menu()
comes before our if statement in the while loop in cli.py
. This means if we aren't somewhere in the loop say ask_question()
the user will automatically get taken back to menu()
. Now your done! The cli should look a little something like this:
Looks great right! Well not really we didn't do any styling, but it works like a charm! If you want to checkout the completed project you can find the repo here!
Posted on June 9, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.