ahmed0saber

Simple calculator using functions in python

May 3rd, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. def add(x, y):
  2.     return x + y
  3.  
  4. def subtract(x, y):
  5.     return x - y
  6.  
  7. def multiply(x, y):
  8.     return x * y
  9.  
  10. def divide(x, y):
  11.     return x / y
  12.  
  13.  
  14. print("Select operation :- \n1-Add\n2-Subtract\n3-Multiply\n4-Divide")
  15.  
  16. while True:
  17.     choice = input("Enter choice(1/2/3/4): ")
  18.     if choice in ('1', '2', '3', '4'):
  19.         num1 = float(input("Enter first number : "))
  20.         num2 = float(input("Enter second number : "))
  21.  
  22.         if choice == '1':
  23.             print(num1, "+", num2, "=", add(num1, num2))
  24.  
  25.         elif choice == '2':
  26.             print(num1, "-", num2, "=", subtract(num1, num2))
  27.  
  28.         elif choice == '3':
  29.             print(num1, "*", num2, "=", multiply(num1, num2))
  30.  
  31.         elif choice == '4':
  32.             print(num1, "/", num2, "=", divide(num1, num2))
  33.         break
  34.     else:
  35.         print("Invalid Input")
Advertisement
Add Comment
Please, Sign In to add comment