Answers for "advanced calculator in python"

2

advanced calculator in python

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 calculator():
  try:
    num1 = float(input("Enter first number?: "))
    for symbol in operations:
      print(symbol)
    operation_symbol = input("Pick an operation: ")
    if operation_symbol not in operations:
      return "Opration choosen is not valid"
    num2 = float(input("Enter second number?: "))
    calculation_function = operations[operation_symbol]
    answer = calculation_function(num1, num2)
  except:
    return "An error happened because of your input try again"
  return(f"{num1} {operation_symbol} {num2} = {answer}")

while True:
  print(calculator())
  to_continue = input("Type 'y' to do more calculations, or type 'n' to exit the app:- ").lower()
  if to_continue == 'y':
    continue
  elif to_continue == "n":
    print("Exiting The App")
    break
Posted by: Guest on July-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language