-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day_14.py
131 lines (102 loc) · 2.09 KB
/
Day_14.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
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
#!/usr/bin/env python
# coding: utf-8
# # Question 51
#
# ### **Question**
#
# > **_Write a function to compute 5/0 and use try/except to catch the exceptions._**
#
# ---
#
# ### Hints
#
# > **_Use try/except to catch exceptions._**
#
# ---
#
# **Solutions:**
# In[1]:
def divide():
return 5 / 0
try:
divide()
except ZeroDivisionError as ze:
print("Why on earth you are dividing a number by ZERO!!")
except:
print("Any other exception")
# ---
#
# # Question 52
#
# ### **Question**
#
# > **_Define a custom exception class which takes a string message as attribute._**
#
# ---
#
# ### Hints
#
# > **_To define a custom exception, we need to define a class inherited from Exception._**
#
# ---
#
# **Solutions:**
# In[ ]:
class CustomException(Exception):
"""Exception raised for custom purpose
Attributes:
message -- explanation of the error
"""
def __init__(self, message):
self.message = message
num = int(input())
try:
if num < 10:
raise CustomException("Input is less than 10")
elif num > 10:
raise CustomException("Input is grater than 10")
except CustomException as ce:
print("The error raised: " + ce.message)
# ---
#
# # Question 53
#
# ### **Question**
#
# > **_Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only._**
#
# > **_Example:
# > If the following email address is given as input to the
# > program:_**
#
# > john@google.com
#
# > **_Then, the output of the program should be:_**
#
# > john
#
# > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
#
# ---
#
# ### Hints
#
# > **_Use \w to match letters._**
#
# ---
#
# **Solutions:**
# In[ ]:
email = "john@google.com"
email = email.split("@")
print(email[0])
# ---
#
# **OR**
# In[ ]:
import re
email = "john@google.com elise@python.com"
pattern = "(\w+)@\w+.com"
ans = re.findall(pattern, email)
print(ans)
# ---