A simple command-line calculator written in Python. Supports four arithmetic operations with input validation, error handling, and a full pytest test suite.
- Addition, subtraction, multiplication, and division
- Input validation — rejects non-numeric arguments with a clear error
- Division-by-zero protection
- Clean CLI powered by
argparse - Comprehensive test coverage with pytest
- Python 3.x
# Clone the repository
git clone https://github.com/terrymclaughlin/copilot-python-calculator.git
cd copilot-python-calculator
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install test dependencies
pip install pytestpython calculator.py <operation> <a> <b>| Operation | Command | Result |
|---|---|---|
| Addition | python calculator.py add 5 3 |
8.0 |
| Subtraction | python calculator.py subtract 10 4 |
6.0 |
| Multiplication | python calculator.py multiply 3 4 |
12.0 |
| Division | python calculator.py divide 10 2 |
5.0 |
# Division by zero
python calculator.py divide 5 0
# Error: Cannot divide by zero
# Invalid input
python calculator.py add foo 3
# error: argument a: invalid float value: 'foo'# Run all tests
pytest test_calculator.py
# Run with verbose output
pytest -v test_calculator.pycopilot-python-calculator/
├── calculator.py # Core operations and CLI entry point
├── test_calculator.py # Pytest test suite
└── .venv/ # Python virtual environment (not committed)