Python: Math Facts

We can use our calculators to test out our math skills, but sometimes it can be more fun to write our own programs! In this tutorial we’ll learn how to create a basic python program that will quiz math facts.

Getting Started

We’ll start with a very simple project with a known answer. Test out the program by entering right, wrong, and nonsensical answers. You can start the program over by pressing the triangular play button.

Observe how the code acts like you’ve entered the wrong answer, even when you’ve done it correctly. This is because your code is treating your input like a word instead of a number and “11” isn’t equal to 11.

You can fix this by explicitly telling the code to treat your input like a number. Replace if answer == 11: with if int(answer) == 11: and run the code again.

Numbers and Strings

It gets a bit annoying needing to rerun the program for all of our test answers, so we’ll wrap the whole thing in a while True: loop. We’ll add a bit of finesse later, but for now this will make testing simpler.

Try out your new code again. Always check it with right and wrong answers, but also try out some unexpected ones. Try entering ‘eleven’ as your answer. An error will appear in pink below the code.

The error is basically saying that you tried to turn a word into an integer and that is no good. So our next step will be to first check to see if the input is numeric. Replace if int(answer) == 11: with

if not answer.isnumeric():
    print("Your answer is not a number.")
elif int(answer) == 11: 

In the first part of the if statement we are making use of a built in string method to determine if our answer is numeric. You can find more methods at w3schools. “elif” stands for “else if” and means that if we don’t meet the first condition, then we should check to see if we meet this one.

Mixing it up

Now, a math fact checker with a hard-coded question doesn’t seem all that great, so we’ll want the program to ask us a new question each time. For that we’re going to need to import a python library that generates random numbers. We’ve added that to the beginning.

After while True: we’re going to want to add some variables for the numbers in our equation. We’ll set them equal to a random integer between 0 and 10. Add:

  number1 = random.randint(0,10)
  number2 = random.randint(0,10)

We need to swap out 5 and 6 for our newly defined numbers. Change the line answer = input('What is 5 + 6?\n') to answer = input("What is " + str(number1) + " + " + str(number2) + "?\n") . Observe that the ‘+’s outside of the quotes are “adding” our strings together. The str() around our variables are casting them to strings (text) so that we can make them become apart of our sentence. Otherwise, the program thinks we are trying to add numbers to words and it fails.

Try your new code out! Try it a few times to make sure that you get a different set of random numbers each time. Does your code recognize right answers? Try your hand at fixing it!

Keeping Score

Now let’s keep track of how many we get right. There are several ways you could do this. We could stop after we get twenty right. We could add a point for each right answer and subtract two for a wrong one, or we could just ask a set number of questions and give a score at the end. We’re going to stop after 20 right answers.

First we need to add a variable to keep score. We’ll want to do that *before* the while loop so that it does not get reset each loop. So add, score = 0 after the import.

Then you’ll want to add score = score + 1 when the player gets the right answer and change your while True to be while score < 20 . This means your code will keep running until the player gets 20 points.

Test out your code to see how well it works. Perhaps add a line telling the player what their score is each time they get a right answer and a message congratulating them on getting 20 right.

Ideas for Expansion

Our finished code is below. You could expand the concept to multiplication, subtraction, and division. You could switch things up and create a vocabulary quiz where you provide a definition and the player needs to spell the vocab word correctly. The possibilities might not quite be endless, but they are plentiful.