advanced calculator in python

Solutions on MaxInterview for advanced calculator in python by the best coders in the world

showing results for - "advanced calculator in python"
Emna
01 Aug 2020
1def add(n1, n2):
2  return n1 + n2
3
4def subtract(n1, n2):
5  return n1 - n2
6
7def multiply(n1, n2):
8  return n1 * n2
9
10def divide(n1, n2):
11  return n1 / n2
12
13operations = {
14  "+": add,
15  "-": subtract,
16  "*": multiply,
17  "/": divide
18}
19
20def calculator():
21  try:
22    num1 = float(input("Enter first number?: "))
23    for symbol in operations:
24      print(symbol)
25    operation_symbol = input("Pick an operation: ")
26    if operation_symbol not in operations:
27      return "Opration choosen is not valid"
28    num2 = float(input("Enter second number?: "))
29    calculation_function = operations[operation_symbol]
30    answer = calculation_function(num1, num2)
31  except:
32    return "An error happened because of your input try again"
33  return(f"{num1} {operation_symbol} {num2} = {answer}")
34
35while True:
36  print(calculator())
37  to_continue = input("Type 'y' to do more calculations, or type 'n' to exit the app:- ").lower()
38  if to_continue == 'y':
39    continue
40  elif to_continue == "n":
41    print("Exiting The App")
42    break
43