-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator.py
More file actions
39 lines (31 loc) · 1.19 KB
/
Copy pathcalculator.py
File metadata and controls
39 lines (31 loc) · 1.19 KB
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
30
31
32
33
34
35
36
37
38
39
import math # imports math module (not required yet, but useful later)
# Ask the user which operation they want
print("Enter an operator: x, +, -, /")
operator = input().strip().lower() # get input, remove spaces, make lowercase
# Check if the operator is valid
if operator not in ["x", "*", "+", "-", "/"]:
print("invalid operator") # tell user if operator is not allowed
else:
# Ask for the first number
num1 = float(input("Enter first number: "))
# Ask for the second number
num2 = float(input("Enter second number: "))
# If the operator is multiplication
if operator == "x" or operator == "*":
result = num1 * num2 # multiply the numbers
# If the operator is addition
elif operator == "+":
result = num1 + num2 # add the numbers
# If the operator is subtraction
elif operator == "-":
result = num1 - num2 # subtract the numbers
# If the operator is division
elif operator == "/":
# Prevent division by zero
if num2 == 0:
print("Error: cannot divide by zero")
exit()
result = num1 / num2 # divide the numbers
# Print the final answer
print("Result:", result)
#done