Skip to content

Commit 54abab0

Browse files
committed
Added Error Handling - Lab and Exercise
1 parent a1c6d1b commit 54abab0

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

Advanced/4.Error Handling/# TODO.txt

Whitespace-only changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
numbers_dictionary = {}
2+
3+
line = input()
4+
5+
while line != "Search":
6+
try:
7+
8+
number_as_string = line
9+
number = int(input())
10+
numbers_dictionary[number_as_string] = number
11+
12+
except ValueError:
13+
print("The variable number must be an integer")
14+
15+
line = input()
16+
17+
line = input()
18+
19+
while line != "Remove":
20+
try:
21+
22+
searched = line
23+
print(numbers_dictionary[searched])
24+
25+
except KeyError:
26+
print("Number does not exist in dictionary" )
27+
28+
line = input()
29+
30+
line = input()
31+
32+
while line != "End":
33+
try:
34+
35+
searched = line
36+
del numbers_dictionary[searched]
37+
38+
except KeyError:
39+
print("Number does not exist in dictionary")
40+
41+
line = input()
42+
43+
print(numbers_dictionary)
44+
45+
46+
47+
''' Error Code '''
48+
# numbers_dictionary = {}
49+
#
50+
# line = input()
51+
#
52+
# while line != "Search":
53+
# number_as_string = line
54+
# number = int(input())
55+
# numbers_dictionary[number_as_string] = number
56+
#
57+
# line = input()
58+
#
59+
# while line != "Remove":
60+
# searched = line
61+
# print(numbers_dictionary[searched])
62+
#
63+
# line = input()
64+
#
65+
# while line != "End":
66+
# searched = line
67+
# del numbers_dictionary[searched]
68+
#
69+
# print(numbers_dictionary)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class NameTooShortError(Exception):
2+
""" Name less than or equal to 4 characters"""
3+
pass
4+
5+
6+
class MustContainAtSymbolError(Exception):
7+
''' Email does not have @ '''
8+
pass
9+
10+
11+
class InvalidDomainError(Exception):
12+
''' the domain is different than .com, .org... '''
13+
pass
14+
15+
16+
email = input()
17+
while email != "End":
18+
good_email = True
19+
20+
if '@' in email:
21+
if len(email.split('@')[0]) <= 4:
22+
good_email = False
23+
raise NameTooShortError("Name must be more than 4 characters")
24+
25+
else:
26+
good_email = False
27+
raise MustContainAtSymbolError("Email must contain @")
28+
29+
if email.split('.')[-1] not in ['com', 'bg', 'org', 'net']:
30+
good_email = False
31+
raise InvalidDomainError("Domain must be one of the following: .com, .bg, .org, .net")
32+
33+
if good_email:
34+
print("Email is valid")
35+
36+
email = input()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
numbers_list = input().split(", ")
2+
result = 1
3+
4+
for i in range(len(numbers_list)):
5+
number = int(numbers_list[i])
6+
if number <= 5:
7+
result *= number
8+
elif 5 < number <= 10:
9+
result /= number
10+
11+
print(result)
12+
13+
14+
''' Error Code '''
15+
# numbers_list = int(input()).split(", ")
16+
# result = 1
17+
#
18+
# for i in range(numbers_list):
19+
# number = numbers_list[i + 1]
20+
# if number <= 5
21+
# result *= number
22+
# elif 5 < number <= 10:
23+
# result /= number
24+
#
25+
# print(total)
26+
27+
28+
29+
30+
31+
32+
''' OLD LAB '''
33+
# numbers_list = input().split(", ")
34+
# result = 1
35+
#
36+
# for index in range(len(numbers_list)):
37+
# number = int(numbers_list[index])
38+
# if number <= 5:
39+
# result *= number
40+
# elif 5 < number <= 10:
41+
# result /= number
42+
#
43+
# print(result)
44+
#
45+
#
46+
# ''' Error Code '''
47+
# # numbers_list = input().split(", ")
48+
# # result = 0
49+
# #
50+
# # for i in range(numbers_list):
51+
# # number = numbers_list[i + 1]
52+
# # if number < 5:
53+
# # result *= number
54+
# # elif number > 5 and number > 10:
55+
# # result /= number
56+
# #
57+
# # print(result)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
try:
2+
3+
text = input()
4+
times = int(input())
5+
print(text * times)
6+
7+
except ValueError:
8+
print("Variable times must be an integer")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ValueCannotBeNegative(Exception):
2+
pass
3+
4+
5+
for _ in range(5):
6+
num = int(input())
7+
if num < 0:
8+
raise ValueCannotBeNegative

0 commit comments

Comments
 (0)