What it shows
Each operation is a separate function, a while loop keeps showing a menu, and an if check stops division by zero. It shows how to organise code into reusable parts.
The science
A function is a named block of code that takes inputs (parameters) and gives back a result with return. Splitting a calculator into add, subtract, multiply and divide makes each part easy to test. A while True loop repeats until break is used. Dividing by zero is impossible, so the program must check first instead of crashing.
Materials
Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- A computer, laptop or phone with a browser (free)
- Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
- A notebook and a pencil
Safety
This project uses safe, everyday materials. Still, read the rules below with a parent before you start.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Create calc.py and write four functions, for example def add(a, b): with return a + b indented below it; do the same for subtract, multiply and divide.
In divide, write if b == 0: return "Cannot divide by zero" before return a / b.
Start the menu loop with while True: and print the choices 1 Add, 2 Subtract, 3 Multiply, 4 Divide, 5 Exit.
Read the choice with choice = input("Choose 1 to 5: ") and add if choice == "5": break to leave the loop.
Ask for the numbers with x = float(input("First number: ")) and y = float(input("Second number: ")).
Use if choice == "1": print("Answer:", add(x, y)) and elif lines for choices 2, 3 and 4.
Add a final else: print("Please choose 1 to 5").
Run it and test with 5 and 0, negative numbers and decimals; fix anything that crashes.
What you should see
The menu appears again and again, each choice prints the right answer for two numbers, dividing by zero gives a polite message instead of an error, and choice 5 ends the program.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What does return do?
It sends a value back to the place where the function was called.
2Why use float for the input?
input gives text; float turns it into a number that can have decimals.
3What does break do?
It leaves the loop immediately.
4Why check for zero?
Dividing by zero gives an error and would stop the program.
Take it further
Add power and remainder operations and use try and except to handle a letter typed instead of a number.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Computer & office
Computer Fundamentals Study Book
The complete study notes, practice questions and glossary for Computer Fundamentals
