Untitled
unknown
plain_text
9 months ago
1.5 kB
5
Indexable
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 "Error! Division by zero."
return x / y
def calculator():
print("Welcome to the Simple Calculator!")
print("Operations:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
while True:
# Get user input
choice = input("Enter the operation (1/2/3/4) or 'q' to quit: ")
if choice == 'q':
print("Exiting the calculator. Goodbye!")
break
if choice in ['1', '2', '3', '4']:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError:
print("Invalid input! Please enter numbers.")
continue
if choice == '1':
print(f"Result: {num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"Result: {num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid choice! Please select a valid operation.")
# Run the calculator
calculator()Editor is loading...
Leave a Comment