Why start with Python
Python is a free programming language that reads almost like plain English. There are no curly braces everywhere and no semicolons at the end of every line: indentation (the space at the start of a line) is what organizes the code. The official site even has a Python for Beginners page with learning resources.
People use Python for small games, data analysis and artificial intelligence alike, so what you learn here keeps paying off, whether in high school science, in CEGEP or in your own side projects.
Step 1: install Thonny or use an online editor
You have two options:
- On a Windows, Mac or Linux computer: install Thonny, a free editor designed for beginners. Python comes built in, so one installer is all you need. It also has a step-by-step debugger that shows the value of your variables while the program runs, which is great for understanding what is going on.
- On a tablet, a Chromebook or a computer where you cannot install anything: use a free online Python editor. Make sure it supports
input()(otherwise the quiz cannot ask you questions), and check with a parent whether signing up has a minimum age.
In Thonny, you type your code in the big area at the top, save it (for example as quiz.py) and click the green triangle button (or press F5) to run it. Output and questions appear in the shell at the bottom.
Step 2: the four ideas you need
- Variables are labeled boxes.
score = 0creates a box called score that holds 0. The=sign means "put this value in the box," not "is equal to." - input() shows a question and waits for the user to type something. Careful: what you get back is always text, even if the user types a number.
- if, elif, else let the program make decisions. To compare two values you use
==(two equal signs), and the lines that depend on the condition are indented by 4 spaces. - Loops repeat instructions.
for number in range(1, 6):runs the block 5 times, with number going 1, 2 and so on up to 5 (6 is excluded). The official Python tutorial covers these tools in depth.
Step 3: the complete program
Copy this code into Thonny or your online editor exactly as written, keeping the indentation. Run it and play one round before reading the explanation.
import random
print("Welcome to the multiplication quiz!")
name = input("What's your name? ")
score = 0
num_questions = 5
for number in range(1, num_questions + 1):
a = random.randint(2, 12)
b = random.randint(2, 12)
correct_answer = a * b
answer = input(f"Question {number}: what is {a} x {b}? ")
if answer.strip() == str(correct_answer):
print("Nice, that's correct!")
score = score + 1
else:
print(f"Oops! The correct answer was {correct_answer}.")
print(f"{name}, your final score is {score} out of {num_questions}.")
if score == num_questions:
print("Perfect score, well done!")
elif score >= 3:
print("Great work, keep it up!")
else:
print("Keep practicing, you'll get better!")Step 4: line-by-line explanation
| Line | What it does |
|---|---|
| 1 | import random loads the random module, which generates random numbers. |
| 3 | print() displays a welcome message. |
| 4 | The program asks for your name and stores it in the variable name. |
| 5 and 6 | The score starts at 0, and the number of questions is set to 5. Change that 5 to make a longer quiz. |
| 8 | The loop repeats the indented lines below it, once per question. |
| 9 and 10 | random.randint(2, 12) picks a whole number from 2 to 12, both ends included (see the random module documentation). |
| 11 | We compute the correct answer. In Python, multiplication is written with *. |
| 12 | Text that starts with f is an f-string: variables inside curly braces are replaced by their values, giving something like "what is 7 x 8?" |
| 13 | Because input() returns text, we turn the correct answer into text with str() before comparing. .strip() removes any spaces typed by accident before or after the number. |
| 14 and 15 | If the answer is right, we congratulate the player and add 1 to the score. |
| 16 and 17 | Otherwise (else), we show the correct answer so the player learns from the mistake. |
| 19 | After the loop (this line is no longer indented), we print the final score. |
| 20 to 25 | Three different messages depending on the score: perfect, 3 or more (>= means "greater than or equal to"), or below 3. |
Why not use int(answer) to turn the answer into a number? That works too, but if someone types a letter, the program crashes with a ValueError. Comparing text with text avoids that: a letter simply counts as a wrong answer.
When it doesn't work: the most common errors
- IndentationError: an indent is missing or does not line up. Every line inside a for or an if must be indented the same way.
- SyntaxError: often a missing colon at the end of a for, if, elif or else line, or a quote that was never closed.
- NameError: a misspelled variable name. To Python, Score and score are two different variables.
- A single = inside an if: comparing needs two.
Read the whole error message: it tells you the line number where things went wrong. Thonny's debugger can also run your program one line at a time.
3 ideas to level up your quiz
- Pick a difficulty. At the start, ask "easy or hard?" with
input(). Use anifto store 5 or 12 in a variable called maximum, then writerandom.randint(2, maximum). - Add a timer. The time module lets you measure how many seconds the round took:
import time start = time.time() # ... the question loop ... elapsed = round(time.time() - start) print(f"Total time: {elapsed} seconds") - Mix the operations. With
random.choice(["+", "-", "x"]), the program picks a random operation. You then need anifto compute the correct answer for whichever one was picked. Bonus challenge: how could you avoid negative answers for subtraction?
What's next?
Programming and math go hand in hand: loops, conditions and random numbers show up everywhere, from probability in high school to statistics in CEGEP. To go further, check out our artificial intelligence course, and read our guide on how to study with AI without cheating before asking a chatbot to write your code for you.
If you would like one-on-one help with programming or math, Stellaire Académie offers tutoring: book a session.
Frequently asked questions
What age can kids start learning Python?+
Many young people start around 12, once they are comfortable reading and typing. Curiosity and the patience to fix mistakes matter more than age.
Do you need to be good at math to code?+
No, not to get started. Coding builds logical thinking, and this quiz shows that a simple project can also help you practice your times tables.
Thonny or an online editor: which should I pick?+
Thonny if you can install software on your computer, since it works offline and its debugger helps you understand your code. An online editor is handy on a Chromebook or at school.
Want personal help? The first call is free.
Book the free call