Python Calculator Guide

1. Introduction

This is a beginner‑friendly Python project designed to teach core programming concepts while gradually introducing more advanced features. This document provides explanations, examples, and learning outcomes.

1.1 What You Need

For this project you will need:

2. Features

3. Full Code

def calculator():
    history = []

    print("Type 'history' to view calculations.")
    print("Type 'q' to quit.\n")

    while True:
        try:
            first = input("Enter the first number: ").strip()

            if first.lower() == "q":
                print("Goodbye!")
                break

            if first.lower() == "history":
                show_history(history)
                continue

            num1 = float(first)

            operator = input(
                "Enter an operator (+, -, *, /, **, %, //): "
            ).strip()

            if operator.lower() == "q":
                print("Goodbye!")
                break

            if operator not in ["+", "-", "*", "/", "**", "%", "//"]:
                print("Invalid operator.\n")
                continue

            second = input("Enter the second number: ").strip()

            if second.lower() == "q":
                print("Goodbye!")
                break

            num2 = float(second)

            if operator in ["/", "%", "//"] and num2 == 0:
                print("You cannot divide by zero.\n")
                continue

            if operator == "+":
                result = num1 + num2
            elif operator == "-":
                result = num1 - num2
            elif operator == "*":
                result = num1 * num2
            elif operator == "/":
                result = num1 / num2
            elif operator == "**":
                result = num1 ** num2
            elif operator == "%":
                result = num1 % num2
            elif operator == "//":
                result = num1 // num2

            calculation = f"{num1:g} {operator} {num2:g} = {result:g}"

            print(f"\nResult: {calculation}\n")

            history.append(calculation)

        except ValueError:
            print("Please enter a valid number.\n")


def show_history(history):
    print("\n---------- HISTORY ----------")

    if not history:
        print("No calculations yet.")
    else:
        for number, calculation in enumerate(history, start=1):
            print(f"{number}. {calculation}")

    print("-----------------------------\n")


calculator()
  

4. How It Works

4.1 User Input

The program asks for:

Special commands:

4.2 Error Handling

The try/except block prevents crashes when users enter invalid numbers.

4.3 Operator Logic

Each operator performs a different calculation:

4.4 History System

Every successful calculation is stored in a list and displayed on request.

5. Example Run

Type 'history' to view calculations.
Type 'q' to quit.

Enter the first number: 12
Enter an operator (+, -, *, /, **, %, //): *
Enter the second number: 5

Result: 12 * 5 = 60

Enter the first number: history

---------- HISTORY ----------
1. 12 * 5 = 60
-----------------------------
  

6. Learning Outcomes

7. Next Steps

8. Conclusion

The Calculator is a perfect progression project for beginners. It starts simple, grows naturally, and ends with a visually impressive GUI version.