Advertisement
JkSoftware

Day 10 - PyCalculator 2

Nov 15th, 2021
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1.  
  2. def add(n1, n2):
  3.     """Add n1 and n2 then returns the result"""
  4.     return n1 + n2
  5.  
  6. def subtract(n1, n2):
  7.     """Subtracts n1 and n2 then returns the result"""
  8.     return n1 - n2
  9.  
  10. def multiply(n1, n2):
  11.     """Multiplies n1 by n2 and returns the result"""
  12.     return n1 * n2
  13.  
  14. def divide(n1, n2):
  15.     """Divides n1 by n2 and returns the result"""
  16.     return n1 / n2
  17.  
  18. operations = {
  19.     "+": add,
  20.     "-": subtract,
  21.     "*": multiply,
  22.     "/": divide
  23. }
  24. is_on = True
  25. def calculator():
  26.     num1 = float(input("First Number: "))
  27.     for key in operations:
  28.         print(key)
  29.  
  30.     while is_on == True:
  31.         symbol = input("Pick an operation: ")
  32.         num2 = float(input("Next Number: "))
  33.         calculate = operations[symbol]
  34.         answer = calculate(num1, num2)
  35.         print(f"{num1} {symbol} {num2} = {answer}")
  36.         if input(f"Type 'y' to continue calculating with {answer} or 'n' to start a new calculation") == "y":
  37.             num1 = answer
  38.         else:
  39.            calculator()
  40. calculator()
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement