From 48c3223bf2ffdb9e009e0be60870f1f65160503a Mon Sep 17 00:00:00 2001 From: Antwan Date: Wed, 21 May 2025 16:17:33 +0200 Subject: [PATCH] This is my first issue so i just tried to change something in the first file i saw to improveits functionality. In this case, I made the calculator a bit mor robust. Now it can actually work with int, not only float and it doesnt shut down if the incorrect value/key is entered. Furtermore i also added a way for the user to exit the program --- Calculator/main.py | 48 +++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/Calculator/main.py b/Calculator/main.py index 599cbc0..51015b8 100644 --- a/Calculator/main.py +++ b/Calculator/main.py @@ -1,25 +1,28 @@ +def format_result(result): + """Return int if result is whole number, else float.""" + if result % 1 == 0: + return int(result) + return result + def add(na1, na2): """ This function will return the sum of two numbers """ - return na1 + na2 - + return format_result(na1 + na2) def exponent(ne1, ne2): """This function will return the power of two numbers""" - return ne1**ne2 + return format_result(ne1 ** ne2) def nth_root(nr1, n): """Getting the nth root of nr1""" - return nr1**(1/n) + return format_result(nr1 ** (1 / n)) def subtract(ns1, ns2): """ This function will return the difference of two numbers """ - return ns1 - ns2 - + return format_result(ns1 - ns2) def multiply(nm1, nm2): """ This function will return the product of two numbers """ - return nm1 * nm2 - + return format_result(nm1 * nm2) def divide(nd1, nd2): """ This function will return the ratio of two numbers """ @@ -39,27 +42,36 @@ def divide(nd1, nd2): def calculator(): """ This function contains the code that will work as you wish to proceed \ an operation """ - num1 = float(input("What's the first number?(to pick √, hold alt + 251 (on numpad)): ")) + + num1 = input("What's the first number?(to pick √, hold alt + 251 (on numpad)): ") for symbol in operations: print(symbol) - should_continue = True - while should_continue: + while True: operation_symbol = input("Pick an operation: ") - num2 = float(input("What's the next number?: ")) - calculation_function = operations[operation_symbol] - answer = calculation_function(num1, num2) + num2 = input("What's the next number?: ") + try: + calculation_function = operations[operation_symbol] + answer = calculation_function(float(num1), float(num2)) + except KeyError: + print("Invalid operation. Please try again.") + except ValueError: + print("Invalid input. Please enter numbers only.") if operation_symbol == "√": print(f"{num2} {operation_symbol} {num1} = {answer}") else: print(f"{num1} {operation_symbol} {num2} = {answer}") - if input(f"Type 'y' to continue calculating with {answer},\ - or type 'n' to start a new calculation: ") == "y": + next = input(f"Type 'y' to continue calculating with {answer},\ + or type 'n' to start a new calculation" + "\nIf you want to shut down the calc, press anything else :" ) + if next == "y": num1 = answer - else: - should_continue = False + elif next == "n": + print("Starting a new calculation...") calculator() + else: + print("Exiting the calculator.") + break calculator()