ETechSavvies

Simple Calculator

Apr 10th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. """
  2.    This is a program that takes two numbers from the user and
  3.    either add, subtract, multiply or divide based on the user's preference
  4.  
  5.    Made by EtechSavvies April 10,2021
  6. """
  7.  
  8. # This function adds two numbers
  9. def add(x, y):
  10.     return x + y
  11.  
  12. # This function subtracts two numbers
  13. def subtract(x, y):
  14.     return x - y
  15.  
  16. # This function multiplies two numbers
  17. def multiply(x, y):
  18.     return x * y
  19.  
  20. # This function divides two numbers
  21. def divide(x, y):
  22.     return x / y
  23.  
  24.  
  25. print("Select operation.")
  26. print("1.Add")
  27. print("2.Subtract")
  28. print("3.Multiply")
  29. print("4.Divide")
  30.  
  31. while True:
  32.     # Take input from the user
  33.     choice = input("Enter choice(1/2/3/4): ")
  34.  
  35.     # Check if choice is one of the four options
  36.     if choice in ('1', '2', '3', '4'):
  37.         num1 = float(input("Enter first number: "))
  38.         num2 = float(input("Enter second number: "))
  39.  
  40.         if choice == '1':
  41.             print(num1, "+", num2, "=", add(num1, num2))
  42.  
  43.         elif choice == '2':
  44.             print(num1, "-", num2, "=", subtract(num1, num2))
  45.  
  46.         elif choice == '3':
  47.             print(num1, "*", num2, "=", multiply(num1, num2))
  48.  
  49.         elif choice == '4':
  50.             print(num1, "/", num2, "=", divide(num1, num2))
  51.         break
  52.     else:
  53.         print("Invalid Input")
Advertisement
Add Comment
Please, Sign In to add comment