-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
63 lines (43 loc) · 1.29 KB
/
Copy pathcalculator.py
File metadata and controls
63 lines (43 loc) · 1.29 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def main():
OPERATIONS = {
'+' : add,
'-' : subtract,
'*' : multiply,
'/' : divide,
}
while True:
try:
first_n = float(input("Enter your first number: "))
second_n = float(input("Enter your second number: "))
operator = get_operator()
result = OPERATIONS[operator](first_n, second_n)
except ValueError:
print("Invalid input!")
continue
except ZeroDivisionError as e:
print(e)
continue
print(f"{first_n} {operator} {second_n} = {result}")
break
# getting operators
def get_operator():
while True:
operator = input("Choose an operator (+, -, *, /): ").strip()
if operator in ['+', '-', '*', '/']:
return operator
else:
print("Invalid operator!")
continue
# creat functions for add, substruct, multiply, divide
def add(a, b):
return a+b
def subtract(a, b):
return a-b
def multiply(a, b):
return a*b
def divide(a, b):
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a/b
if __name__ == "__main__":
main()