thecodex.expert · The Codex Family of Knowledge
Tier 0 · Absolute Beginner · Python Project

Calculator

A working calculator that adds, subtracts, multiplies, and divides. Learn functions, choices, and safe maths.

🧠 Teaches how to think spoonfed, every age Last verified:

1 The Problem

We want a calculator: the user picks an operation, enters two numbers, and gets the result. It teaches organising code into functions, choosing between options, and guarding against the one maths error that crashes programs — dividing by zero.

Where this shows up: every app does arithmetic somewhere — totals, averages, conversions, scoring. And the habit of wrapping each operation in a named function is exactly how larger programs stay organised.

2 How to Think About It

The plan, before any code:

The plan — in plain English
1. Offer the four operations. → 2. Ask which one, and for two numbers. → 3. Do the maths with a small function for each operation. → 4. Show the result — but first check we are not dividing by zero, which is impossible.

add

subtract

multiply

divide

No

Yes

Show 4 operations

Ask which + two numbers

Which operation?

a + b

a - b

a x b

b is zero?

a / b

Show error

Show result

3 The Build — explained part by part

Here is the complete calculator. Each part is explained below.

Pythoncalculator.py
# A calculator with one function per operation.

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    if b == 0:
        return "Cannot divide by zero"
    return a / b

print("1. Add  2. Subtract  3. Multiply  4. Divide")
choice = input("Choose an operation (1-4): ")

a = float(input("First number: "))
b = float(input("Second number: "))

if choice == "1":
    print(f"Result: {add(a, b)}")
elif choice == "2":
    print(f"Result: {subtract(a, b)}")
elif choice == "3":
    print(f"Result: {multiply(a, b)}")
elif choice == "4":
    print(f"Result: {divide(a, b)}")
else:
    print("Please choose 1 to 4.")
What each part does — in plain words
def add(a, b): — define a reusable function named add that takes two numbers and returns their sum. We make one for each operation. A function is a named, reusable piece of logic.

def divide(a, b): — division needs a guard: if b is 0, we return a friendly message instead of crashing, because dividing by zero is mathematically impossible.

choice = input(...) and a, b = float(...) — ask which operation, then read two numbers as decimals.

if / elif / else — run the function that matches the choice. {add(a, b)} calls the function and drops its result straight into the message.
Common mistakes — and how to avoid them
✗ Forgetting the divide-by-zero guard — entering 0 as the second number crashes with ZeroDivisionError.
✓ Always check if b == 0 before dividing.
✗ Comparing choice == 1 (number) instead of "1" (text).
input() returns text, so compare to "1" in quotes.
✗ Using int() for the numbers — then 2.5 becomes 2.
✓ Use float() so decimals work.

4 Test & Prove Each Part

Each operation is its own function, which makes testing clean — we check each against answers we already know, including the divide-by-zero guard.

Addition: 2 + 3 is 5
Subtraction: 10 - 4 is 6
Multiplication: 6 x 7 is 42
Division by zero returns a safe message, not a crash
Pythontest_calculator.py
from calculator import add, subtract, multiply, divide


def test_add():
    """2 + 3 is 5."""
    assert add(2, 3) == 5


def test_subtract():
    """10 - 4 is 6."""
    assert subtract(10, 4) == 6


def test_multiply():
    """6 times 7 is 42."""
    assert multiply(6, 7) == 42


def test_divide():
    """10 / 2 is 5."""
    assert divide(10, 2) == 5


def test_divide_by_zero():
    """Dividing by zero returns a message, not a crash."""
    assert divide(5, 0) == "Cannot divide by zero"

Run with pytest -v. Because each operation is a named function, the tests import them directly. The divide-by-zero test proves our guard works — the difference between a toy and a reliable tool.

5 The Interface

INPUToperation + two numbers1-4, then two numbers
What it expects
Choose an operation (1-4): 1
First number: 8
Second number: 5
OUTPUTresultthe answer
What it returns
Result: 13.0

6 Run It & Automate It

Run it locally
python3 calculator.py
Pick an operation, enter two numbers, get the result.

Jenkins runs the tests automatically — each line explained below.

What you should see when it works
Terminala real run
1. Add  2. Subtract  3. Multiply  4. Divide
Choose an operation (1-4): 1
First number: 8
Second number: 5
Result: 13.0
If it breaks — how to fix it
🚨 ZeroDivisionError.
Your divide function is missing the zero guard. Add if b == 0: return "Cannot divide by zero".
🚨 It always says “Please choose 1 to 4”.
You compared to numbers instead of text. Use "1", "2", etc.
GroovyJenkinsfile
// Jenkinsfile — automated tests on every change.
pipeline {
    agent any
    stages {
        stage('Get the code') { steps { checkout scm } }       // download the code
        stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } }  // test tool
        stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } }   // run every test
    }
    post {
        success { echo 'All tests passed.' }
        failure { echo 'A test failed — look above.' }
    }
}
🎯 Try this next — make it yours
  1. Keep calculating. Loop so the user can do many sums. (Teaches: a while loop.)
  2. More operations. Add power and remainder. (Teaches: more functions.)
  3. Use a dictionary. Map choices to functions to shrink the if/elif chain. (Teaches: dictionaries of functions.)
What you learned
You learned to organise code into functions (one job each), choose between options, and guard against errors like dividing by zero — the habits that keep bigger programs clean and safe. Related: Functions & Scope, Control Flow.