Creating a simple quiz game using python
Tags: python, beginners, tutorial, webdev
Published: 2023-06-09 09:43:43

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](https://www.w3schools.com/sql/). If you want to follow along you can use [this](https://github.com/drwomble/quiz-game-template) repository I created as a template. First off we will want to get started with the `Quiz` class:

![Quiz class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzi6ifq6cnu5xp9pouu7.png)

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:


![SQL methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42s24cu7ppqldsjuqntm.png)

![find_by_question method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6yco4hxgp0a5qkadu0q.png)
Now we can work on the `cli.py` file:

![cli.py file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ncanxpc77ab1fwcrx5d.png)
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:

![welcome and menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpeog2n0i6mxbrxf4oe7.png)
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:


![ask_question method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3gun3aw7lp76e3gc77k.png)

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:

![handle_next](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mrss7bg08gjwgjl1mds.png)
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:

![create_new()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xmcldu380ey1tjbwkcu.png)
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:

![exit_cli and invalid inputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hncl8wshyxhz05ahiy4l.png)
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:

![cli gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdlqsg9bjiarr16qwuj7.gif)
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](https://github.com/drwomble/quiz-game)!