-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_calculator.py
More file actions
33 lines (30 loc) · 909 Bytes
/
simple_calculator.py
File metadata and controls
33 lines (30 loc) · 909 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
30
31
32
33
# Simple Calculator
def calculate(num1, num2, operator):
match operator:
case '+':
return num1 + num2
case '-':
return num1 - num2
case '*':
return num1 * num2
case '/':
if num2 != 0:
return num1 / num2
else:
print("Error: Division by zero")
exit()
case _:
print("Error: Invalid operator")
exit()
def main():
try:
num1 = float(input("Enter the first number: "))
operator = input("Enter the operator (+, -, *, /): ")
num2 = float(input("Enter the second number: "))
result = calculate(num1, num2, operator)
print(f"The result is: {result}")
except ValueError:
print("Error: Invalid input. Please enter numeric values.")
exit()
if __name__ == "__main__":
main()