Simple Calculator Implementation in Python
unknown
python
a month ago
1.7 kB
3
Indexable
Never
#Calculator import art def add(n1, n2): return n1 + n2 def subtract(n1, n2): return n1 - n2 def multiply(n1, n2): return n1 * n2 def divide(n1, n2): return n1 / n2 operations = { "+": add, "-": subtract, "*": multiply, "/": divide, } def calculate_result(num1, op, num2): if op == "+": result = operations["+"](num1, num2) return result elif op == "-": result = operations["-"](num1, num2) return result elif op == "*": result = operations["*"](num1, num2) return result elif op == "/": result = operations["/"](num1, num2) return result def continue_calc(orig_result): print("+\n-\n*\n/") operation = input("Pick an operation: ") n2 = float(input("What is the next number?")) result = calculate_result(orig_result, operation, n2) print(f"{orig_result} + {n2} = {result}") cont = input(f"Type 'y' to continue calculating with {result}, or type 'n' to start a new calculation ").lower() if cont == "y": continue_calc(result) elif cont == "n": print("\n" * 20) print(art.logo) new_clac() def new_clac(): n1 = float(input("What is the first number?: ")) print("+\n-\n*\n/") operation = input("Pick an operation: ") n2 = float(input("What is the next number? ")) result = calculate_result(n1, operation, n2) print(f"{n1} + {n2} = {result}") cont = input(f"Type 'y' to continue calculating with {result}, or type 'n' to start a new calculation ").lower() if cont == "y": continue_calc(result) elif cont == "n": print("\n" * 20) print(art.logo) new_clac() print(art.logo) new_clac()
Leave a Comment