Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def add(x, y):
- return x + y
- def subtract(x, y):
- return x - y
- def multiply(x, y):
- return x * y
- def divide(x, y):
- if y == 0:
- return "Cannot divide by zero!"
- else:
- return x / y
- def power(x, y):
- return x ** y
- def sqrt(x):
- if x < 0:
- return "Invalid input for square root of a negative number!"
- else:
- return math.sqrt(x)
- def logarithm(x, base):
- if x <= 0 or base <= 0 or base == 1:
- return "Invalid input for logarithm!"
- else:
- return math.log(x, base)
- print("Select operation:")
- print("1. Addition")
- print("2. Subtraction")
- print("3. Multiplication")
- print("4. Division")
- print("5. Exponentiation")
- print("6. Square Root")
- print("7. Logarithm")
- while True:
- choice = input("Enter choice (1/2/3/4/5/6/7): ")
- if choice in ('1', '2', '3', '4', '5', '6', '7'):
- if choice in ('1', '2', '3', '4', '5'):
- num1 = float(input("Enter first number: "))
- num2 = float(input("Enter second number: "))
- if choice == '1':
- print("Result:", add(num1, num2))
- elif choice == '2':
- print("Result:", subtract(num1, num2))
- elif choice == '3':
- print("Result:", multiply(num1, num2))
- elif choice == '4':
- print("Result:", divide(num1, num2))
- elif choice == '5':
- print("Result:", power(num1, num2))
- elif choice == '6':
- num = float(input("Enter a number: "))
- print("Result:", sqrt(num))
- elif choice == '7':
- num = float(input("Enter a number: "))
- base = float(input("Enter the base for logarithm: "))
- print("Result:", logarithm(num, base))
- break
- else:
- print("Invalid input. Please enter a valid number (1/2/3/4/5/6/7).")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement