-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
29 lines (26 loc) · 848 Bytes
/
Copy pathcalculator.py
File metadata and controls
29 lines (26 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Step 1: Get two numbers from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Step 2: Convert inputs to numbers
num1 = float(num1)
num2 = float(num2)
# Step 3: Ask for the operation
operation = input("Enter an operation (+, -, *, /): ")
# Step 4: Perform the operation
if operation == "+":
result = num1 + num2
print(f"{num1} + {num2} = {result}")
elif operation == "-":
result = num1 - num2
print(f"{num1} - {num2} = {result}")
elif operation == "*":
result = num1 * num2
print(f"{num1} * {num2} = {result}")
elif operation == "/":
if num2 != 0:
result = num1 / num2
print(f"{num1} / {num2} = {result}")
else:
print("Error: Cannot divide by zero.")
else:
print("Invalid operation. Please enter +, -, * or /.")