CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 1 • Lesson 10

🧮 Mini Project: Calculator

Congratulations! You've reached the final Python lesson of Quest 1. Now you'll build a Calculator – a complete program that combines everything you've learned.

"A calculator is a perfect first project – it's practical, familiar, and exercises core programming skills."

📋 Project Requirements

🧠 Code Walkthrough

We'll build the calculator step by step:

  1. Define arithmetic functions – each takes two numbers and returns the result.
  2. Display a menu – print the options.
  3. Get user choice – use input() inside a loop.
  4. Get numbers – use try/except to handle non‑numeric inputs.
  5. Execute the chosen operation – call the appropriate function.
  6. Handle division by zero – check the second number before dividing.
  7. Loop until exit – break when user selects 5.
calculator.py
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 "Error: Division by zero"
    return a / b

while True:
    print("\n=== Calculator ===")
    print("1. Add")
    print("2. Subtract")
    print("3. Multiply")
    print("4. Divide")
    print("5. Exit")

    try:
        choice = int(input("Choose an option: "))
    except ValueError:
        print("Invalid input. Please enter a number.")
        continue

    if choice == 5:
        print("Goodbye!")
        break

    if choice not in [1, 2, 3, 4]:
        print("Invalid choice. Please select 1-5.")
        continue

    try:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
    except ValueError:
        print("Please enter valid numbers.")
        continue

    if choice == 1:
        result = add(num1, num2)
    elif choice == 2:
        result = subtract(num1, num2)
    elif choice == 3:
        result = multiply(num1, num2)
    elif choice == 4:
        result = divide(num1, num2)

    print(f"Result: {result}")

🚀 Live Web Demo (Simulates the Python Calculator)

Try a working calculator built with HTML/CSS/JS – the logic mirrors the Python version.

0

The web demo uses the same four operations as your Python calculator.

✨ Challenge: Extend the Calculator

Add the following features to your Python calculator:

❤️ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

☕ Buy Me a Coffee

🎉 You've Completed Quest 1!

You've built 10 lessons in Python. Next: Quest 2 – Object-Oriented Programming & More.

Start Quest 2 →

(Coming soon – check back or buy Pro Pack for early access)