def calculator():
print("Welcome to the Python Calculator!")
print("Select an operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Exponentiation (^)")
print("6. Modulus (%)")
print("7. Exit")
while True:
# Get user input for operation
try:
choice = int(input("\nEnter the number of the operation (1-7): "))
except ValueError:
print("Invalid input! Please enter a number between 1 and 7.")
continue
if choice == 7:
print("Thank you for using the calculator. Goodbye!")
break
if choice not in range(1, 7):
print("Invalid choice! Please select a valid operation.")
continue
# Get numbers from user
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError:
print("Invalid input! Please enter numeric values.")
continue
# Perform the selected operation
if choice == 1:
print(f"Result: {num1} + {num2} = {num1 + num2}")
elif choice == 2:
print(f"Result: {num1} - {num2} = {num1 - num2}")
elif choice == 3:
print(f"Result: {num1} * {num2} = {num1 * num2}")
elif choice == 4:
if num2 == 0:
print("Error: Division by zero is not allowed.")
else:
print(f"Result: {num1} / {num2} = {num1 / num2}")
elif choice == 5:
print(f"Result: {num1} ^ {num2} = {num1 ** num2}")
elif choice == 6:
print(f"Result: {num1} % {num2} = {num1 % num2}")
if __name__ == "__main__":
calculator()