Advertisement
JkSoftware

Day 10 - PyCalculator 1

Nov 15th, 2021
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def add(n1, n2):
  2.     """Add n1 and n2 then returns the result"""
  3.     return n1 + n2
  4.  
  5. def subtract(n1, n2):
  6.     """Subtracts n1 and n2 then returns the result"""
  7.     return n1 - n2
  8.  
  9. def multiply(n1, n2):
  10.     """Multiplies n1 by n2 and returns the result"""
  11.     return n1 * n2
  12.  
  13. def divide(n1, n2):
  14.     """Divides n1 by n2 and returns the result"""
  15.     return n1 / n2
  16.  
  17. operations = {
  18.     "+": add,
  19.     "-": subtract,
  20.     "*": multiply,
  21.     "/": divide
  22. }
  23.  
  24. num1 = int(input("First Number: "))
  25. for key in operations:
  26.     print(key)
  27. symbol = input("Pick an operation from the above list: ")
  28. num2 = int(input("Second Number: "))
  29.  
  30. calculate = operations[symbol]
  31.  
  32. answer = calculate(num1, num2)
  33. print(f"{num1} {symbol} {num2} = {answer}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement