A simple command-line calculator that performs basic arithmetic operations (addition, subtraction, multiplication, division) with support for decimal and negative numbers.
- ✅ Addition, subtraction, multiplication, division
- ✅ Support for decimal numbers
- ✅ Support for negative numbers
- ✅ Error handling for division by zero
- ✅ Input validation with clear error messages
- ✅ Type hints on all functions
- ✅ Comprehensive test coverage
- Python 3.8 or later
uvpackage manager
# Clone the repository
git clone <repo-url>
cd sdd-basic-cli-calculator
# Create virtual environment and install dependencies
uv sync
# Activate virtual environment
source .venv/bin/activate # On Windows: .venv\Scripts\activate# Addition
python -m src.cli 2 + 3
# Output: 5
# Subtraction
python -m src.cli 10 - 4
# Output: 6
# Multiplication
python -m src.cli 6 * 7
# Output: 42
# Division
python -m src.cli 20 / 4
# Output: 5python -m src.cli 3.5 + 2.1
# Output: 5.6
python -m src.cli 10.5 - 3.2
# Output: 7.3python -m src.cli -5 + 3
# Output: -2
python -m src.cli 10 \* -2
# Output: -20# Division by zero
python -m src.cli 10 / 0
# Output (stderr): Error: Division by zero
# Exit code: 1
# Invalid input
python -m src.cli abc + 5
# Output (stderr): Error: Invalid operand 'abc' - must be a number
# Exit code: 1
# Invalid operator
python -m src.cli 5 ^ 3
# Output (stderr): Error: Invalid operator '^' - must be one of: +, -, *, /
# Exit code: 1
# Missing arguments
python -m src.cli 5 +
# Output (stderr): Error: Invalid number of arguments - expected 3, got 2
# Exit code: 1# Run all tests
pytest
# Run with coverage
pytest --cov=src
# Run specific test file
pytest tests/unit/test_calculator.py
# Run with verbose output
pytest -vsrc/
├── calculator.py # Core calculator logic
└── cli.py # CLI interface
tests/
├── unit/
│ └── test_calculator.py # Unit tests
└── integration/
└── test_cli.py # Integration tests
pyproject.toml # Project configuration
All code includes type hints:
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b- Write test first (Red)
- Run test, verify it fails
- Implement code (Green)
- Run test, verify it passes
- Refactor if needed (Refactor)
- Commit with clear message
- Pure functions with no side effects
- All functions have type hints
- Each operation is independently testable
- Raises
ValueErrorfor invalid operations
- Parses command-line arguments
- Validates input
- Calls appropriate calculator function
- Formats and displays results
- Handles errors gracefully
- Unit tests: Test calculator functions in isolation
- Integration tests: Test CLI with subprocess calls
- Coverage goal: >80% of code
MIT