[text] Calc

Viewer

  1. def add(x, y):
  2.     """Function to add two numbers"""
  3.     return x + y
  4.  
  5. def subtract(x, y):
  6.     """Function to subtract two numbers"""
  7.     return x - y
  8.  
  9. def multiply(x, y):
  10.     """Function to multiply two numbers"""
  11.     return x * y
  12.  
  13. def divide(x, y):
  14.     """Function to divide two numbers"""
  15.     if y == 0:
  16.         return "Error! Division by zero."
  17.     return x / y
  18.  
  19. print("Select operation:")
  20. print("1. Add")
  21. print("2. Subtract")
  22. print("3. Multiply")
  23. print("4. Divide")
  24.  
  25. while True:
  26.     choice = input("Enter choice (1/2/3/4): ")
  27.  
  28.     if choice in ('1', '2', '3', '4'):
  29.         num1 = float(input("Enter first number: "))
  30.         num2 = float(input("Enter second number: "))
  31.  
  32.         if choice == '1':
  33.             print("Result:", add(num1, num2))
  34.         elif choice == '2':
  35.             print("Result:", subtract(num1, num2))
  36.         elif choice == '3':
  37.             print("Result:", multiply(num1, num2))
  38.         elif choice == '4':
  39.             print("Result:", divide(num1, num2))
  40.     else:
  41.         print("Invalid input")
  42.  
  43.     again = input("Do you want to perform another calculation? (yes/no): ")
  44.     if again.lower() != 'yes':
  45.         break
  46.  

Editor

You can edit this paste and save as new:


File Description
  • Calc
  • Paste Code
  • 28 Mar-2024
  • 1.16 Kb
You can Share it: