From 2ff131b9077dd0e8ac403d69448e87c53c39ec39 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Thu, 2 Apr 2026 14:53:35 +0800 Subject: [PATCH] Add calculator with basic arithmetic operations - Implement add, subtract, multiply, divide functions - Handle division by zero with proper exception - Include comprehensive test suite - Use ASCII characters for cross-platform compatibility --- .gitignore | 13 +++++++ py_match.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 .gitignore create mode 100644 py_match.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3090454 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.claude/ +hlm_2/ +__pycache__/ +*.pyc +*.pyo +*.pyd +.Python +env/ +venv/ +*.egg-info/ +dist/ +build/ +.DS_Store \ No newline at end of file diff --git a/py_match.py b/py_match.py new file mode 100644 index 0000000..3d22654 --- /dev/null +++ b/py_match.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +""" +Simple calculator with basic arithmetic operations. +Handles division by zero and provides simple testing. +""" + +def add(a: float, b: float) -> float: + """Return the sum of a and b.""" + return a + b + +def subtract(a: float, b: float) -> float: + """Return a minus b.""" + return a - b + +def multiply(a: float, b: float) -> float: + """Return a multiplied by b.""" + return a * b + +def divide(a: float, b: float) -> float: + """ + Return a divided by b. + + Raises: + ZeroDivisionError: if b is 0 + """ + if b == 0: + raise ZeroDivisionError("Cannot divide by zero") + return a / b + +def test_calculator(): + """Test all calculator functions.""" + print("Testing calculator functions...") + + # Test addition + result = add(5, 3) + expected = 8 + assert result == expected, f"Addition failed: {result} != {expected}" + print(f"[OK] add(5, 3) = {result}") + + # Test subtraction + result = subtract(10, 4) + expected = 6 + assert result == expected, f"Subtraction failed: {result} != {expected}" + print(f"[OK] subtract(10, 4) = {result}") + + # Test multiplication + result = multiply(7, 6) + expected = 42 + assert result == expected, f"Multiplication failed: {result} != {expected}" + print(f"[OK] multiply(7, 6) = {result}") + + # Test division + result = divide(15, 3) + expected = 5 + assert result == expected, f"Division failed: {result} != {expected}" + print(f"[OK] divide(15, 3) = {result}") + + # Test division by zero + try: + divide(10, 0) + print("[ERROR] Division by zero should have raised an error") + return False + except ZeroDivisionError as e: + print(f"[OK] divide(10, 0) raised: {e}") + + # Test with floats + result = add(2.5, 3.7) + expected = 6.2 + assert abs(result - expected) < 0.0001, f"Float addition failed: {result} != {expected}" + print(f"[OK] add(2.5, 3.7) = {result}") + + print("\n[PASSED] All tests passed!") + return True + +def main(): + """Main function to demonstrate calculator usage.""" + print("Calculator Demo") + print("-" * 50) + + # Demonstrate basic operations + a, b = 20, 5 + + print(f"a = {a}, b = {b}") + print(f"a + b = {add(a, b)}") + print(f"a - b = {subtract(a, b)}") + print(f"a * b = {multiply(a, b)}") + print(f"a / b = {divide(a, b)}") + + print("\n" + "-" * 50) + + # Show division by zero handling + print("Testing division by zero protection:") + try: + result = divide(a, 0) + except ZeroDivisionError as e: + print(f"Caught expected error: {e}") + +if __name__ == "__main__": + # Run tests first + if test_calculator(): + print("\n" + "=" * 50) + main() \ No newline at end of file