-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_14exceptionhandling.py
More file actions
112 lines (72 loc) · 2.56 KB
/
Copy pathday_14exceptionhandling.py
File metadata and controls
112 lines (72 loc) · 2.56 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#----------------------------------------------------------------------------------------------------------------
#day 14-Exception Handling
#----------------------------------------------------------------------------------------------------------------
#1.try and except
try:
num=int(input("Enter a number:"))
print(num)
except ValueError:
print("Please Enter a valid integer.")
#------------------------------------------------------------------------------------------------------------------
#2.division by zero
try:
a=int(input())
b=int(input())
res = a/b
print("Result:" ,res)
except ZeroDivisionError:
print("Cannot divide by zero.")
#------------------------------------------------------------------------------------------------------------------
#3.Square number with exception handling
try:
a = int(input("Enter a number:"))
sq = a*a
print("Square of the given number:",sq)
except ValueError:
print("Invalid Input")
print("Enter a Valid Input.")
#--------------------------------------------------------------------------------------------------------------------
#4.Multiple Exceptions
try:
num = [100,200,300,400,500]
index = int(input("Enter Index:"))
print("Index=", num[index])
except ValueError:
print("Enter a Valid Input.")
except IndexError:
print("Index out of range.")
#---------------------------------------------------------------------------------------------------------------------
#5.Positive number check with else block
try:
num = int(input("Enter a number:"))
except ValueError:
print("Invalid Input")
print("Please Enter a valid input")
else:
print("You Entered:", num)
if (num>0):
print("The given number is Positive.")
#--------------------------------------------------------------------------------------------------------------------
#6. Age Validator with finally
try:
age = int(input("Enter age:"))
except ValueError:
print("Invalid Input")
else:
print("The Age of the user is", age)
if (age >= 18):
print("The User is Eligible for Voting")
else:
print("OOPS!! The User is not Eligible for Voting")
finally:
print("Program Finished")
#---------------------------------------------------------------------------------------------------------------------
#7. Age validator with raise
try:
age=int(input("Enter age:"))
if age < 0:
raise ValueError("Age cannot be negative")
print("Age:", age)
except ValueError as e:
print(e)
#---------------------------------------------------------------------------------------------------------------------