From 9e1a54e50f099aebb4ff0807a22ecf81a5565d30 Mon Sep 17 00:00:00 2001 From: elfroot-sj Date: Wed, 12 Nov 2025 19:42:41 +0100 Subject: [PATCH] feat: added calculator.py --- src/calculator.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/calculator.py b/src/calculator.py index 5eb03460..a6de35f2 100644 --- a/src/calculator.py +++ b/src/calculator.py @@ -1,12 +1,40 @@ # -# Write a program that given two numbers as input make the main operations. +# Write a program that, given two numbers as input, performs the main operations. # # Output: -# Insert first number: 4 -# Insert second number: 2 +# Enter the first number: 4 +# Enter the second number: 2 # -# SUM: 6 +# Sum: 6 # Difference: 2 # Multiplication: 8 # Division: 2 # +# ------------------------------------------------------------------------------- +from typing import Optional + +# functions + +def sum(a: float, b: float) -> float: + return a + b + +def diff(a: float, b: float) -> float: + return a - b + +def mult(a: float, b: float) -> float: + return a * b + +def div(a: float, b: float) -> Optional[float]: + if b == 0: + print("Division by 0 is not allowed.") + return None + return round(a / b, 2) + +# inputs +num1 = float(input("Enter the first number: ")) +num2 = float(input("Enter the second number: ")) + +print("Sum: ", sum(num1, num2)) +print("Difference: ", diff(num1, num2)) +print("Multiplication: ", mult(num1, num2)) +print("Division: ", div(num1, num2)) \ No newline at end of file