diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/main.py index 7f9aacaec..39d6bd597 100644 --- a/solutions/basic_calculator/main.py +++ b/solutions/basic_calculator/main.py @@ -1,5 +1,3 @@ - - """ A module for performing basic arithmetic operations: addition, subtraction, multiplication, and division. @@ -11,11 +9,13 @@ Author: Ana Isabel Murillo """ + def calculate(operation: str, num1: float, num2: float) -> float: """Performs the specified arithmetic operation on two numbers. Parameters: - operation: str, the operation to perform ('add', 'subtract', 'multiply', 'divide') + operation: str, the operation to perform + ('add', 'subtract', 'multiply', 'divide') num1: float, the first number num2: float, the second number @@ -38,15 +38,18 @@ def calculate(operation: str, num1: float, num2: float) -> float: ... ValueError: Cannot divide by zero. """ - if operation == 'add': + if operation == "add": return num1 + num2 - elif operation == 'subtract': + elif operation == "subtract": return num1 - num2 - elif operation == 'multiply': + elif operation == "multiply": return num1 * num2 - elif operation == 'divide': + elif operation == "divide": if num2 == 0: raise ValueError("Cannot divide by zero.") return num1 / num2 else: - raise ValueError(f"Invalid operation '{operation}'. Valid operations are: add, subtract, multiply, divide.") + raise ValueError( + f"Invalid operation '{operation}'. Valid operations are: " + "add, subtract, multiply, divide." + ) diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py index 58550519b..1e9d80b76 100644 --- a/solutions/tests/test_basic_calculator.py +++ b/solutions/tests/test_basic_calculator.py @@ -10,45 +10,46 @@ Author: Ana Isabel Murillo """ -from solutions.basic_calculator.main import calculate import unittest +from solutions.basic_calculator.main import calculate + class TestCalculate(unittest.TestCase): """Test suite for the calculate function.""" def test_addition(self): """It should correctly add two numbers.""" - self.assertEqual(calculate('add', 5, 3), 8.0) - self.assertEqual(calculate('add', -5, -3), -8.0) - self.assertEqual(calculate('add', 0, 5), 5.0) + self.assertEqual(calculate("add", 5, 3), 8.0) + self.assertEqual(calculate("add", -5, -3), -8.0) + self.assertEqual(calculate("add", 0, 5), 5.0) def test_subtraction(self): """It should correctly subtract the second number from the first.""" - self.assertEqual(calculate('subtract', 10, 4), 6.0) - self.assertEqual(calculate('subtract', -5, -5), 0.0) - self.assertEqual(calculate('subtract', 0, 5), -5.0) + self.assertEqual(calculate("subtract", 10, 4), 6.0) + self.assertEqual(calculate("subtract", -5, -5), 0.0) + self.assertEqual(calculate("subtract", 0, 5), -5.0) def test_multiplication(self): """It should correctly multiply two numbers.""" - self.assertEqual(calculate('multiply', 7, 3), 21.0) - self.assertEqual(calculate('multiply', -2, 3), -6.0) - self.assertEqual(calculate('multiply', 0, 10), 0.0) + self.assertEqual(calculate("multiply", 7, 3), 21.0) + self.assertEqual(calculate("multiply", -2, 3), -6.0) + self.assertEqual(calculate("multiply", 0, 10), 0.0) def test_division(self): """It should correctly divide the first number by the second.""" - self.assertEqual(calculate('divide', 9, 3), 3.0) - self.assertEqual(calculate('divide', -6, 3), -2.0) - self.assertAlmostEqual(calculate('divide', 7, 2), 3.5) + self.assertEqual(calculate("divide", 9, 3), 3.0) + self.assertEqual(calculate("divide", -6, 3), -2.0) + self.assertAlmostEqual(calculate("divide", 7, 2), 3.5) def test_division_by_zero(self): """It should raise a ValueError when attempting to divide by zero.""" with self.assertRaises(ValueError) as context: - calculate('divide', 5, 0) + calculate("divide", 5, 0) self.assertEqual(str(context.exception), "Cannot divide by zero.") def test_invalid_operation(self): """It should raise a ValueError for an invalid operation.""" with self.assertRaises(ValueError) as context: - calculate('invalid_op', 5, 3) + calculate("invalid_op", 5, 3) self.assertIn("Invalid operation", str(context.exception))