Repository navigation
Expand file tree
/
Copy pathpython_custom_exceptions.py
More file actions
261 lines (196 loc) · 5.9 KB
/
Copy pathpython_custom_exceptions.py
File metadata and controls
261 lines (196 loc) · 5.9 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# ============================================================
# ASSIGNMENT: PYTHON CUSTOM EXCEPTIONS
# 5 PRACTICE TASKS
# ============================================================
# ============================================================
# TASK 1: AGE VERIFICATION
#
# Create a custom exception named InvalidAgeError.
# If age is below 18, raise InvalidAgeError.
# Otherwise, display "Eligible to register".
# ============================================================
class InvalidAgeError(Exception):
pass
try:
age = int(input("Enter your age: "))
if age < 18:
raise InvalidAgeError("Age must be 18 or above.")
print("Eligible to register")
except InvalidAgeError as e:
print("Invalid Age:", e)
# SAMPLE OUTPUT 1:
#
# Enter your age: 16
# Invalid Age: Age must be 18 or above.
#
# SAMPLE OUTPUT 2:
#
# Enter your age: 20
# Eligible to register
# ============================================================
# TASK 2: EXAM MARKS VALIDATION
#
# Create a custom exception named InvalidMarksError.
# If marks are below 0 or above 100, raise the exception.
# If marks are below 35, display "Failed".
# Otherwise, display "Passed".
# ============================================================
class InvalidMarksError(Exception):
pass
try:
marks = int(input("\nEnter your marks out of 100: "))
if marks < 0 or marks > 100:
raise InvalidMarksError(
"Marks must be between 0 and 100."
)
if marks < 35:
print("Failed")
else:
print("Passed")
except InvalidMarksError as e:
print("Invalid Marks:", e)
# SAMPLE OUTPUT 1:
#
# Enter your marks out of 100: 120
# Invalid Marks: Marks must be between 0 and 100.
#
# SAMPLE OUTPUT 2:
#
# Enter your marks out of 100: 25
# Failed
#
# SAMPLE OUTPUT 3:
#
# Enter your marks out of 100: 75
# Passed
# ============================================================
# TASK 3: BANK WITHDRAWAL
#
# Create two custom exceptions:
# 1. InsufficientBalanceError
# 2. InvalidAmountError
#
# Initial balance = 5000.
# If withdrawal amount is greater than balance,
# raise InsufficientBalanceError.
# If amount is zero or negative, raise InvalidAmountError.
# Otherwise, subtract the amount and display remaining balance.
# ============================================================
class InsufficientBalanceError(Exception):
pass
class InvalidAmountError(Exception):
pass
balance = 5000
try:
amount = float(input("\nEnter withdrawal amount: "))
if amount <= 0:
raise InvalidAmountError(
"Withdrawal amount must be greater than zero."
)
if amount > balance:
raise InsufficientBalanceError(
"Insufficient balance in your account."
)
balance = balance - amount
print("Withdrawal successful")
print("Remaining balance:", balance)
except InvalidAmountError as e:
print("Invalid Amount:", e)
except InsufficientBalanceError as e:
print("Bank Error:", e)
# SAMPLE OUTPUT 1:
#
# Enter withdrawal amount: 6000
# Bank Error: Insufficient balance in your account.
#
# SAMPLE OUTPUT 2:
#
# Enter withdrawal amount: -500
# Invalid Amount: Withdrawal amount must be greater than zero.
#
# SAMPLE OUTPUT 3:
#
# Enter withdrawal amount: 2000
# Withdrawal successful
# Remaining balance: 3000.0
# ============================================================
# TASK 4: PRODUCT STOCK MANAGEMENT
#
# Create two custom exceptions:
# 1. OutOfStockError
# 2. InvalidQuantityError
#
# Available stock = 10 units.
# If requested quantity is greater than stock,
# raise OutOfStockError.
# If quantity is zero or negative, raise InvalidQuantityError.
# Otherwise, update stock and display remaining quantity.
# ============================================================
class OutOfStockError(Exception):
pass
class InvalidQuantityError(Exception):
pass
stock = 10
try:
quantity = int(input("\nEnter quantity to purchase: "))
if quantity <= 0:
raise InvalidQuantityError(
"Quantity must be greater than zero."
)
if quantity > stock:
raise OutOfStockError(
"Requested quantity is not available in stock."
)
stock = stock - quantity
print("Purchase successful")
print("Remaining stock:", stock)
except InvalidQuantityError as e:
print("Invalid Quantity:", e)
except OutOfStockError as e:
print("Stock Error:", e)
# SAMPLE OUTPUT 1:
#
# Enter quantity to purchase: 15
# Stock Error: Requested quantity is not available in stock.
#
# SAMPLE OUTPUT 2:
#
# Enter quantity to purchase: 0
# Invalid Quantity: Quantity must be greater than zero.
#
# SAMPLE OUTPUT 3:
#
# Enter quantity to purchase: 4
# Purchase successful
# Remaining stock: 6
# ============================================================
# TASK 5: PASSWORD VALIDATION
#
# Create a custom exception named WeakPasswordError.
# If password contains fewer than 8 characters,
# raise WeakPasswordError.
# Otherwise, display "Password accepted".
# ============================================================
class WeakPasswordError(Exception):
pass
try:
password = input("\nEnter your password: ")
if len(password) < 8:
raise WeakPasswordError(
"Password must contain at least 8 characters."
)
print("Password accepted")
except WeakPasswordError as e:
print("Weak Password:", e)
# SAMPLE OUTPUT 1:
#
# Enter your password: abc123
# Weak Password: Password must contain at least 8 characters.
#
# SAMPLE OUTPUT 2:
#
# Enter your password: alekhya123
# Password accepted
# ============================================================
# END OF ASSIGNMENT
# ============================================================