Intro to Python Assignment
Basic Calculator Program
Create a simple Python program that asks the user to input two numbers and a mathematical operation (addition, subtraction, multiplication, or division). Perform the operation based on the user's input and print the result. Example: If a user inputs 10, 5, and +, your program should display 10 + 5 = 15.
def calculator(): try: # Get user input num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) operation = input("Enter an operation (+, -, *, /): ")
# Perform the operation
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
if num2 == 0:
print("Error: Division by zero is not allowed.")
return
result = num1 / num2
else:
print("Invalid operation. Please enter +, -, *, or /.")
return
# Display the result
print(f"{num1} {operation} {num2} = {result}")
except ValueError:
print("Error: Please enter valid numbers.")
calculator()