Print any times table, then take a 5-question quiz with random questions and get a score at the end.
Learning your times tables takes practice, so why not build a program to help? In this project you will make a Times Table Trainer. The player chooses a table, like the 7 times table, and the program prints the whole thing. Then it switches into quiz mode and asks five random questions to test their memory.
This project shows off one of the things computers do best: repeating work without getting tired. A for loop with range() can print a whole times table in just two lines of code, and it would be just as easy to print the 1000 times table!
You will also use the random module to make every quiz different, keep a score with a variable, and give the player an encouraging message at the end. It is a program you can actually use for your homework, and a great one to share with younger brothers or sisters.
We ask the player which times table they want to practise. input() gives back text, so we use int() to turn it into a whole number we can multiply with.
print("âī¸ Times Table Trainer")
table = int(input("Which times table do you want to practise? "))
print("Great choice! Let's practise the", table, "times table.")
âī¸ Times Table Trainer Which times table do you want to practise? 7 Great choice! Let's practise the 7 times table.
range(1, 11) gives us the numbers 1 to 10, because the last number is never included. The for loop runs once for each number, and we print a line like 7 x 3 = 21. The * sign does the multiplying.
table = 7
for number in range(1, 11):
print(table, "x", number, "=", table * number)
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
random.randint(1, 10) picks a number from 1 to 10. We build the question as a string by joining pieces with +, using str() to turn the numbers into text. Then we ask it with input().
import random
table = 7
number = random.randint(1, 10)
question_text = "What is " + str(table) + " x " + str(number) + "? "
answer = int(input(question_text))
print("You answered", answer)
What is 7 x 8? 56 You answered 56
The right answer is table * number. If the player's answer matches it, we cheer. If not, we kindly tell them the correct answer, because seeing the right answer helps you remember it next time.
import random
table = 7
number = random.randint(1, 10)
answer = int(input("What is " + str(table) + " x " + str(number) + "? "))
if answer == table * number:
print("â
Correct!")
else:
print("â Oops! It's", table * number)
What is 7 x 8? 56 â Correct!
We wrap the question in a for loop that runs 5 times with range(1, 6). The loop variable question tells us which question we are on, so we can show it in the prompt. Every correct answer adds 1 to score.
import random
table = 7
score = 0
for question in range(1, 6):
number = random.randint(1, 10)
question_text = "Question " + str(question) + ": What is " + str(table) + " x " + str(number) + "? "
answer = int(input(question_text))
if answer == table * number:
print("â
Correct!")
score = score + 1
else:
print("â Oops! It's", table * number)
print("You scored", score, "out of 5.")
Question 1: What is 7 x 8? 56 â Correct! Question 2: What is 7 x 9? 63 â Correct! Question 3: What is 7 x 8? 57 â Oops! It's 56 Question 4: What is 7 x 8? 56 â Correct! Question 5: What is 7 x 9? 63 â Correct! You scored 4 out of 5.
Different scores deserve different messages. We use if, elif and else to choose one: a big celebration for 5, praise for 3 or more, and encouragement for everyone else. Learning is all about practice!
score = 4
if score == 5:
print("đ Perfect! You're a times table champion!")
elif score >= 3:
print("đ Great work! A little more practice and you'll be perfect.")
else:
print("đĒ Keep practising, you'll get there!")
đ Great work! A little more practice and you'll be perfect.
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
import random
print("âī¸ Times Table Trainer")
table = int(input("Which times table do you want to practise? "))
print()
print("Here is the", table, "times table:")
for number in range(1, 11):
print(table, "x", number, "=", table * number)
print()
print("Now it's quiz time! 5 questions.")
score = 0
for question in range(1, 6):
number = random.randint(1, 10)
question_text = "Question " + str(question) + ": What is " + str(table) + " x " + str(number) + "? "
answer = int(input(question_text))
if answer == table * number:
print("â
Correct!")
score = score + 1
else:
print("â Oops! It's", table * number)
print()
print("You scored", score, "out of 5.")
if score == 5:
print("đ Perfect! You're a times table champion!")
elif score >= 3:
print("đ Great work! A little more practice and you'll be perfect.")
else:
print("đĒ Keep practising, you'll get there!")
This is what it looks like when someone plays. The words after each question are what the player typed.
âī¸ Times Table Trainer Which times table do you want to practise? 7 Here is the 7 times table: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 Now it's quiz time! 5 questions. Question 1: What is 7 x 8? 56 â Correct! Question 2: What is 7 x 9? 63 â Correct! Question 3: What is 7 x 8? 57 â Oops! It's 56 Question 4: What is 7 x 8? 56 â Correct! Question 5: What is 7 x 9? 63 â Correct! You scored 4 out of 5. đ Great work! A little more practice and you'll be perfect.
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.