Advertisement
SkidScripts

Basic Calculator (python)

Nov 29th, 2023
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 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.     if y == 0:
  12.         return "Cannot divide by zero!"
  13.     else:
  14.         return x / y
  15.  
  16. print("Select operation:")
  17. print("1. Addition")
  18. print("2. Subtraction")
  19. print("3. Multiplication")
  20. print("4. Division")
  21.  
  22. while True:
  23.     choice = input("Enter choice (1/2/3/4): ")
  24.  
  25.     if choice in ('1', '2', '3', '4'):
  26.         num1 = float(input("Enter first number: "))
  27.         num2 = float(input("Enter second number: "))
  28.  
  29.         if choice == '1':
  30.             print("Result:", add(num1, num2))
  31.         elif choice == '2':
  32.             print("Result:", subtract(num1, num2))
  33.         elif choice == '3':
  34.             print("Result:", multiply(num1, num2))
  35.         elif choice == '4':
  36.             print("Result:", divide(num1, num2))
  37.         break
  38.     else:
  39.         print("Invalid input. Please enter a valid number (1/2/3/4).")
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement