Times-table tester
Make a program that tests you on any times table, then tells you which ones to practise.
Start here: open pygwide. On the home page, choose Tutorials, then Python Toolkit, then Lesson 1: Times-table tester.
This lesson takes about an hour. The extensions take about 30 minutes more.
What you will learn
- How to keep lots of things together in a list.
- How to add to a list, and count what is in it.
- How to repeat lines for each thing in a list with a
forloop. - How to count with
range().
What you need from before
int(input())and f-strings, from Python Basics, Lesson 2.ifandelse, from Python Basics, Lesson 3.- Choices in square brackets, from Python Basics, Lesson 4.
whileloops, from Python Basics, Lesson 5.
Step 1: Lists
A list keeps lots of things together, in order. You met one in the fortune teller. Type this and run it:
snacks = ["crisps", "apple", "popcorn"]
print(snacks)
print(snacks[0])
print(snacks[2])
Each thing in a list has a number, called its index. Python starts
counting at 0, so snacks[0] is the first snack, "crisps".
In Scratch
A Python list is like a Scratch list. snacks[0] is like item 1 of
snacks, but Python counts from 0.
Step 2: Add to a list
.append() adds something to the end of a list. len() says how many
things are in it:
snacks = ["crisps", "apple", "popcorn"]
snacks.append("grapes")
print(snacks)
print(f"There are {len(snacks)} snacks.")
A list can start empty, with nothing between the brackets: wrong = [].
In Scratch
.append() is like add thing to list, and len() is like
length of list.
Step 3: For each thing in a list
A for loop runs its lines once for each thing in a list:
snacks = ["crisps", "apple", "popcorn"]
for snack in snacks:
print(f"I like {snack}!")
Each time round, snack holds the next thing in the list.
Step 4: Count with range
range(1, 11) gives the numbers from 1 up to 10. It stops just before
the second number. Use it in a for loop to print the 7 times table:
for number in range(1, 11):
print(f"{number} x 7 = {number * 7}")
In Scratch
A for loop over range() is like repeat 10, with a variable that
goes up by 1 each time.
Step 5: The tester
Now replace your code with the tester. It asks which table to practise,
then asks each question in turn. Each wrong answer goes in the wrong
list:
print("Times-table tester")
table = int(input("Which times table do you want to practise? "))
wrong = []
for number in range(1, 11):
answer = int(input(f"What is {number} x {table}? "))
if answer == number * table:
print("Correct!")
else:
print(f"Not quite. It is {number * table}.")
wrong.append(number)
Step 6: What to practise
After the loop, say the score, and list the ones to practise. The score is 10 take away the number of wrong answers:
print()
print(f"You got {10 - len(wrong)} out of 10.")
if len(wrong) > 0:
print("Practise these:")
for number in wrong:
print(f"{number} x {table} = {number * table}")
Check your code
# Times-table tester
print("Times-table tester")
table = int(input("Which times table do you want to practise? "))
wrong = []
for number in range(1, 11):
answer = int(input(f"What is {number} x {table}? "))
if answer == number * table:
print("Correct!")
else:
print(f"Not quite. It is {number * table}.")
wrong.append(number)
print()
print(f"You got {10 - len(wrong)} out of 10.")
if len(wrong) > 0:
print("Practise these:")
for number in wrong:
print(f"{number} x {table} = {number * table}")
Extensions
Mix them up, and try again
random.shuffle() mixes up a list. list(range(1, 11)) makes a list of
the numbers 1 to 10, so you can shuffle them and ask the questions in any
order.
Then, at the end, ask each question you got wrong one more time.
One way to do it
# Times-table tester, in a random order, with a second try
import random
table = int(input("Which times table do you want to practise? "))
numbers = list(range(1, 11))
random.shuffle(numbers)
wrong = []
for number in numbers:
answer = int(input(f"What is {number} x {table}? "))
if answer == number * table:
print("Correct!")
else:
print(f"Not quite. It is {number * table}.")
wrong.append(number)
if len(wrong) > 0:
print()
print("Let's try the ones you missed again.")
for number in wrong:
answer = int(input(f"What is {number} x {table}? "))
if answer == number * table:
print("Correct! You've got it now.")
else:
print(f"It is {number * table}. Keep practising!")
Up to 12
Change the tester to go up to 12 x the table. What do you need to change in
range()? What else needs to change?
Extra bits
Fix the bug
This should print 1 to 10, but it stops at 9. Why? How do you fix it?
for number in range(1, 10):
print(number)
Export your project
pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.
Next time, choose ⇪ Import a project on the home page to carry on with it.