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
- Functions – separate functions for add, subtract, multiply, divide.
- User menu – display a menu (1: Add, 2: Subtract, 3: Multiply, 4: Divide, 5: Exit).
- Loop – keep the program running until the user chooses exit.
- Error handling – catch division by zero and non‑numeric input.
- Input validation – ensure the user selects a valid menu option.
- Dictionary (bonus) – map menu options to function names or use a simple if‑elif.
🧠 Code Walkthrough
We'll build the calculator step by step:
- Define arithmetic functions – each takes two numbers and returns the result.
- Display a menu – print the options.
- Get user choice – use
input()inside a loop. - Get numbers – use
try/exceptto handle non‑numeric inputs. - Execute the chosen operation – call the appropriate function.
- Handle division by zero – check the second number before dividing.
- 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:
- Power – calculate a^b.
- Modulo – remainder after division.
- History – store the last 5 results and display them.
- Retry – after each calculation, ask if the user wants to continue.
def power(a, b): return a ** b
def modulo(a, b): return a % b
# Add to menu and if-elif chain.
def modulo(a, b): return a % b
# Add to menu and if-elif chain.
❤️ 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)