←Previous page

12GAME PROJECTS · CALCULATOR

Build your own calculator.

You type two numbers and a sign. PyBot picks the right math and shows the answer.

IN REAL LIFE

Calculators are everywhere.

Many machines take numbers, choose an operation, and show a result.

The shop register

It adds up prices and takes away the money you pay.

Sharing a pizza

8 slices for 4 friends: divide, and each one gets 2.

The phone calculator

Type 0 after the ÷ sign and it shows a message, not an answer.

THE PLAN

The calculator in four steps.

Each step uses something you already know.

Ask two numbers

Turn each answer into a number. float keeps decimals like 2.5.

input + float

Ask the sign

The player types +, -, * or /. It stays text.

input

Choose the math

Compare the sign with == and do the matching operation.

if / elif / else

Pack it in a function

calculate gets the numbers and the sign and returns the answer. It checks for 0 first.

def + return

RUN IT

Try the whole calculator.

Guess first. Then run real Python and check.

1

Guess the result

a = float(input("First number? ")) sign = input("Sign (+ - * /)? ") b = float(input("Second number? ")) print("Answer:", calculate(a, sign, b))

PyBot's answers: 8, then *, then 2

What is the last line?

calculator.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change the answers to 7, / and 0. What does PyBot say?

BUILD IT

Build it step by step.

Fix one part at a time. Run after each step.

0Completed
11Not tried
0Review
01

Step 1: two real numbers

8 + 2 shows 82. Turn both answers into numbers with float().

Goal output
First number? 8
Second number? 2
Sum: 10.0
OUTPUT

02

Step 2: choose the math

The calculator knows + and -, but not *. Add an elif for * before else.

Goal output
First number? 6
Sign (+ - * /)? *
Second number? 7
Answer: 42.0
OUTPUT

03

Step 3: protect from 0

Dividing by 0 stops the program with an error. Make divide return the message when b is 0.

Goal output
Answer: 4.0
Answer: Oops! You can't divide by 0.
OUTPUT

YOUR TURN

Questions about the calculator.

Try each tiny question. A wrong answer becomes something to review.

04

Without float()

a = input("First number? ") b = input("Second number? ") print(a + b)

PyBot's answers: 3, then 4

What does the last line show?

05

Which branch?

if sign == "+": print(a + b) elif sign == "-": print(a - b) else: print("?")

a is 9.0, b is 4.0 and sign is "-"

What will Python show?

06

Compare the sign

PyBot must check if the sign is a plus.

Which line is right?

07

Why check for 0?

if b == 0: return "Oops! You can't divide by 0." return a / b

Why does the calculator check b first?

08

What does return do?

print("Answer:", calculate(8, "*", 2))

calculate ends with return a * b. Where does the 16 go?

09

A strange sign

In the full calculator, the player types 5, then x, then 3.

What does PyBot answer?

10

Half a pizza

print(7 / 2)

What will Python show?

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A function can be a machine.

Numbers and a sign go in, if/elif picks the math, and return sends the answer out. Check for 0 before you divide.