Chapter 2: Control Flow / Lesson 13

Console Calculator Program

🎯 Project: Console Calculator

In this lesson, you'll build a fully functional calculator program that performs basic arithmetic operations. This project will help you practice using if statements, handling user input, and managing edge cases.

The calculator will accept two numbers and an operator (+, -, *, /) and display the result. We'll also implement error handling for invalid inputs and division by zero.

Project Requirements

Your calculator should meet these requirements:

  • Accept two numbers from the user (or use predefined values for testing)
  • Accept an operator (+, -, *, /)
  • Perform the correct calculation based on the operator
  • Handle division by zero with a clear error message
  • Handle invalid operators with an error message
  • Display the result in a clear format

Basic Calculator Structure

Here's a basic structure using if-elif statements to handle different operators:

calculator.py
num1 = 10 num2 = 5 operator = "+" if operator == "+": result = num1 + num2 print(f"{num1} + {num2} = {result}") elif operator == "-": result = num1 - num2 print(f"{num1} - {num2} = {result}") elif operator == "*": result = num1 * num2 print(f"{num1} * {num2} = {result}") elif operator == "/": if num2 == 0: print("Error: Division by zero is not allowed!") else: result = num1 / num2 print(f"{num1} / {num2} = {result}") else: print(f"Error: Invalid operator '{operator}'")

Enhanced Calculator with Match Statement

You can also use a match statement for cleaner code (Python 3.10+):

calculator_match.py
num1 = 10 num2 = 5 operator = "+" match operator: case "+": result = num1 + num2 print(f"{num1} + {num2} = {result}") case "-": result = num1 - num2 print(f"{num1} - {num2} = {result}") case "*": result = num1 * num2 print(f"{num1} * {num2} = {result}") case "/": if num2 == 0: print("Error: Division by zero!") else: result = num1 / num2 print(f"{num1} / {num2} = {result}") case _: print(f"Error: Unknown operator '{operator}'")

Error Handling Tips

⚠️ Division by Zero

Always check if the divisor is zero before dividing. Division by zero will crash your program with a ZeroDivisionError.

✅ User-Friendly Messages

Provide clear, helpful error messages when something goes wrong. This makes your program more user-friendly and easier to debug.

Advanced: Multiple Operations

You can extend the calculator to handle multiple operations or additional operators:

advanced_calc.py
num1 = 16 num2 = 2 operator = "%" # Modulo (remainder) match operator: case "+": result = num1 + num2 case "-": result = num1 - num2 case "*": result = num1 * num2 case "/": result = num1 / num2 if num2 != 0 else "Error" case "%": # Modulo result = num1 % num2 if num2 != 0 else "Error" case "**": # Exponentiation result = num1 ** num2 case _: result = "Invalid operator" print(f"{num1} {operator} {num2} = {result}")
🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...