-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_try_example.py
More file actions
42 lines (29 loc) · 840 Bytes
/
Copy path15_try_example.py
File metadata and controls
42 lines (29 loc) · 840 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
34
35
36
37
38
39
40
41
42
# try ==> in this block we write the task we want to do
# except ==> in this block we write what to do if exception occurs
# finally ==> this block will execute as soon as the TRY EXCEPT blocks are over
# calculator
def add(a, b):
return (a + b)
def sub(a, b):
return (a - b)
def mul(a, b):
return (a * b)
def div(a, b):
return (a / b)
def calculate(a, b):
print(add(a, b))
print(sub(a, b))
print(mul(a, b))
print(div(a, b))
try:
num1 = (int) (input("enter first number: "))
num2 = (int) (input("enter 2nd number: "))
calculate(num1, num2)
except ValueError:
print("That is an invalid input")
except ZeroDivisionError:
print("Cannot divide by 0")
except:
print("Something wrong happened")
finally:
print("Calculator exited")