From a6b651ea8f8437fd848aad90a82dc852a5e6a7a7 Mon Sep 17 00:00:00 2001 From: Prerna Pal <147034640+prernads@users.noreply.github.com> Date: Sat, 21 Oct 2023 15:13:23 +0530 Subject: [PATCH] Create calculator(prerna) --- calculator(prerna) | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 calculator(prerna) diff --git a/calculator(prerna) b/calculator(prerna) new file mode 100644 index 0000000..bfe56da --- /dev/null +++ b/calculator(prerna) @@ -0,0 +1,46 @@ +# Function to perform addition +def add(x, y): + return x + y + +# Function to perform subtraction +def sub(x, y): + return x - y + +# Function to perform multiplication +def mult(x, y): + return x * y + +# Function to perform division +def div(x, y): + if y == 0: + return "Cannot divide by zero" + return x / y +while True: + print("Options:") + print("Enter 'add' for addition") + print("Enter 'subtract' for subtraction") + print("Enter 'multiply' for multiplication") + print("Enter 'divide' for division") + print("Enter 'quit' to end the program") + + user_input = input(": ") + + if user_input == "quit": + break + elif user_input in ("add", "subtract", "multiply", "divide"): + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if user_input == "add": + print("Result: ", add(num1, num2)) + elif user_input == "subtract": + print("Result: ", sub(num1, num2)) + elif user_input == "multiply": + print("Result: ", mult(num1, num2)) + elif user_input == "divide": + print("Result: ", div(num1, num2)) + else: + print("Invalid input") + + +