-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson_23.py
34 lines (31 loc) · 1.15 KB
/
lesson_23.py
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
# Exceptions
def calculator(num1, num2, operator):
"""Simple number clculator. Can addition, subtruction, mulitplication, division."""
if operator == '+':
return f"{num1} {operator} {num2} = {num1 + num2}"
elif operator == '-':
return f"{num1} {operator} {num2} = {num1 - num2}"
elif operator == '*':
return f"{num1} {operator} {num2} = {num1 * num2}"
elif operator == '/':
try:
return f"{num1} {operator} {num2} = {num1 / num2}"
except ZeroDivisionError as ex:
raise ArithmeticError() from None
else:
print("Operation can't be done with your operator. Use '+', '-', '*', '/'")
def main():
num1 = input('Write first argument: ')
try:
num1 = int(num1)
num2 = input('Write second argument: ')
try:
num2 = int(num2)
operator = input('Write operator please: ')
print(calculator(num1, num2, operator))
except ValueError as ex:
print("Second argument must be a number")
except ValueError as ex:
print("First argument must be a number")
if __name__ == '__main__':
main()