|
1 | 1 |
|
| 2 | +# Question 51 |
| 3 | + |
| 4 | +### **Question** |
| 5 | + |
| 6 | +> ***Write a function to compute 5/0 and use try/except to catch the exceptions.*** |
| 7 | +
|
| 8 | +---------------------- |
| 9 | +### Hints |
| 10 | +> ***Use try/except to catch exceptions.*** |
| 11 | +
|
| 12 | +---------------------- |
| 13 | + |
| 14 | +**Main author's Solution: Python 2** |
| 15 | +```python |
| 16 | +def throws(): |
| 17 | + return 5/0 |
| 18 | + |
| 19 | +try: |
| 20 | + throws() |
| 21 | +except ZeroDivisionError: |
| 22 | + print "division by zero!" |
| 23 | +except Exception, err: |
| 24 | + print 'Caught an exception' |
| 25 | +finally: |
| 26 | + print 'In finally block for cleanup' |
| 27 | +``` |
| 28 | +---------------- |
| 29 | +**My Solution: Python 3** |
| 30 | +```python |
| 31 | +#to be written |
| 32 | + |
| 33 | +``` |
| 34 | +--------------------- |
| 35 | + |
| 36 | + |
| 37 | +# Question 52 |
| 38 | + |
| 39 | +### **Question** |
| 40 | + |
| 41 | +> ***Define a custom exception class which takes a string message as attribute.*** |
| 42 | +
|
| 43 | +---------------------- |
| 44 | +### Hints |
| 45 | +> ***To define a custom exception, we need to define a class inherited from Exception.*** |
| 46 | +
|
| 47 | +---------------------- |
| 48 | + |
| 49 | +**Main author's Solution: Python 2** |
| 50 | +```python |
| 51 | +class MyError(Exception): |
| 52 | + """My own exception class |
| 53 | +
|
| 54 | + Attributes: |
| 55 | + msg -- explanation of the error |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self, msg): |
| 59 | + self.msg = msg |
| 60 | + |
| 61 | +error = MyError("something wrong") |
| 62 | + |
| 63 | +``` |
| 64 | +---------------- |
| 65 | +**My Solution: Python 3** |
| 66 | +```python |
| 67 | +#to be written |
| 68 | + |
| 69 | +``` |
| 70 | +--------------------- |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +# Question 53 |
| 76 | + |
| 77 | +### **Question** |
| 78 | + |
| 79 | +> ***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.*** |
| 80 | +
|
| 81 | +> ***Example: |
| 82 | +If the following email address is given as input to the |
| 83 | +program:*** |
| 84 | + |
| 85 | + |
| 86 | +``` |
| 87 | +john@google.com |
| 88 | +``` |
| 89 | +> ***Then, the output of the program should be:*** |
| 90 | +
|
| 91 | +``` |
| 92 | +john |
| 93 | +``` |
| 94 | +> ***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 95 | +
|
| 96 | +---------------------- |
| 97 | +### Hints |
| 98 | +> ***Use \w to match letters.*** |
| 99 | +
|
| 100 | +---------------------- |
| 101 | + |
| 102 | +**Main author's Solution: Python 2** |
| 103 | +```python |
| 104 | +import re |
| 105 | +emailAddress = raw_input() |
| 106 | +pat2 = "(\w+)@((\w+\.)+(com))" |
| 107 | +r2 = re.match(pat2,emailAddress) |
| 108 | +print r2.group(1) |
| 109 | +``` |
| 110 | +---------------- |
| 111 | +**My Solution: Python 3** |
| 112 | +```python |
| 113 | +#to be written |
| 114 | + |
| 115 | +``` |
| 116 | +--------------------- |
0 commit comments