|
| 1 | +# exercise 3.4 from unit 3 |
| 2 | +''' |
| 3 | +Ido maintains a baking blog online for food lovers. Every surfer who wants to connect to the blog is obliged to choose a username and password as follows. |
| 4 | +
|
| 5 | +Username: |
| 6 | +
|
| 7 | +Must consist of the following legal characters: English letters, numbers and an underscore. |
| 8 | +It must be between 3 and 16 characters long. |
| 9 | +The password: |
| 10 | +
|
| 11 | +Must consist of at least 8 characters, and up to a maximum of 40 characters in length. |
| 12 | +Must contain at least one of the following mandatory characters: one uppercase English letter, one lowercase English letter, |
| 13 | + one number and one special sign such as an exclamation mark (to perform the task you can use string.punctuation). |
| 14 | +You must help Ido write a code that checks whether the username and password entered by surfers meet the conditions. |
| 15 | +
|
| 16 | +Write a function called check_input defined as follows: |
| 17 | +
|
| 18 | +def check_input(username, password): |
| 19 | +
|
| 20 | +The function accepts two parameters of type string: username and password. The function prints "OK" to the screen if the username and |
| 21 | + password are correct (that is, meet the conditions presented earlier). |
| 22 | +
|
| 23 | +If the username or password (or both) do not meet the defined conditions we would like to throw a detailed exception describing what |
| 24 | + went wrong. For this purpose, fulfill the following exceptions: |
| 25 | +
|
| 26 | +Exception named UsernameContainsIllegalCharacter - describes a username that contains illegal characters. |
| 27 | +Exception named UsernameTooShort - describes a username consisting of less than 3 characters. |
| 28 | +Exception named UsernameTooLong - describes a username consisting of more than 16 characters. |
| 29 | +An exception named PasswordMissingCharacter - describes a password that does not contain at least one of the mandatory characters. |
| 30 | +Exception named PasswordTooShort - describes a password consisting of less than 8 characters. |
| 31 | +Exception named PasswordTooLong - describes a password consisting of more than 40 characters. |
| 32 | +Please note: each exception is implemented in a separate class that inherits from the Exception superclass. |
| 33 | +
|
| 34 | +We have updated the code of the check_input function so that in a situation where it receives as input an invalid username or password, |
| 35 | + an appropriate exception will be thrown. |
| 36 | +
|
| 37 | +If everything is correct, the function will print OK as usual. |
| 38 | +
|
| 39 | +For your convenience, we have compiled for you inputs with which you can test the code you wrote. |
| 40 | +
|
| 41 | +check_input("1", "2") |
| 42 | +check_input("0123456789ABCDEFG", "2") |
| 43 | +check_input("A_a1.", "12345678") |
| 44 | +check_input("A_1", "2") |
| 45 | +check_input("A_1", "ThisIsAQuiteLongPasswordAndHonestlyUnnecessary") |
| 46 | +check_input("A_1", "abcdefghijklmnop") |
| 47 | +check_input("A_1", "ABCDEFGHIJLKMNOP") |
| 48 | +check_input("A_1", "ABCDEFGhijklmnop") |
| 49 | +check_input("A_1", "4BCD3F6h1jk1mn0p") |
| 50 | +check_input("A_1", "4BCD3F6.1jk1mn0p") |
| 51 | +All inputs except the last one are incorrect. Make sure that for each of the inputs one exception is thrown that describes |
| 52 | + the problem with it. The order of priority of the thrown exception is determined according to the order of the clauses in the question. |
| 53 | +
|
| 54 | +Write a main main function that receives the user's username and password for the blog. Incorporate in the main function block a call |
| 55 | + to the check_input function to check whether the input meets the conditions. |
| 56 | +
|
| 57 | +If the input is not correct, ensure that a detailed message describing the problem with the input is printed to the user. Then ask |
| 58 | + for more input until you get proper input. |
| 59 | +
|
| 60 | +Below is a sample run: |
| 61 | +
|
| 62 | +To view an accessible code snippet |
| 63 | +check_input("1", "2") |
| 64 | +check_input("0123456789ABCDEFG", "2") |
| 65 | +check_input("A_a1.", "12345678") |
| 66 | +check_input("A_1", "2") |
| 67 | +check_input("A_1", "ThisIsAQuiteLongPasswordAndHonestlyUnnecessary") |
| 68 | +check_input("A_1", "abcdefghijklmnop") |
| 69 | +check_input("A_1", "ABCDEFGHIJLKMNOP") |
| 70 | +check_input("A_1", "ABCDEFGhijklmnop") |
| 71 | +check_input("A_1", "4BCD3F6h1jk1mn0p") |
| 72 | +check_input("A_1", "4BCD3F6.1jk1mn0p") |
| 73 | +
|
| 74 | +Improved the UsernameContainsIllegalCharacter and PasswordMissingCharacter exceptions to contain a more accurate description of the problem. |
| 75 | +
|
| 76 | +So far the UsernameContainsIllegalCharacter exception class has printed a generic message: |
| 77 | +
|
| 78 | +The username contains an illegal character |
| 79 | +
|
| 80 | +Now, add a description of the illegal character and its position in the string to the UsernameContainsIllegalCharacter class. |
| 81 | +
|
| 82 | +Below is a sample run: |
| 83 | +
|
| 84 | +To view an accessible code snippet |
| 85 | +check_input("A_a1.", "12345678") |
| 86 | +So far the PasswordMissingCharacter exception class has printed a generic message: |
| 87 | +
|
| 88 | +The password is missing a character |
| 89 | +
|
| 90 | +Now, create 4 subclasses that inherit from the PasswordMissingCharacter class, where each class will describe a missing character in the password. |
| 91 | +
|
| 92 | +Note, the classes must extend the use of the __str__ method of the superclass, so that when printing it will only add in parentheses additional |
| 93 | + detail about the missing character. That is, it must use the method of the superclass and add to it. |
| 94 | +
|
| 95 | +Here are additions for certain characters: |
| 96 | +
|
| 97 | +check_input("A_1", "abcdefghijklmnop") |
| 98 | +check_input("A_1", "ABCDEFGHIJLKMNOP") |
| 99 | +check_input("A_1", "ABCDEFGhijklmnop") |
| 100 | +check_input("A_1", "4BCD3F6h1jk1mn0p") |
| 101 | +The password is missing a character (Uppercase) |
| 102 | +The password is missing a character (Lowercase) |
| 103 | +The password is missing a character (Digit) |
| 104 | +The password is missing a character (Special) |
| 105 | +''' |
| 106 | + |
| 107 | +import string |
| 108 | + |
| 109 | + |
| 110 | +class UsernameContainsIllegalCharacter(Exception): |
| 111 | + def __init__(self, username, illegal_char, index): |
| 112 | + self.username = username |
| 113 | + self.illegal_char = illegal_char |
| 114 | + self.index = index |
| 115 | + |
| 116 | + def __str__(self): |
| 117 | + return f"The username '{self.username}' contains the illegal character '{self.illegal_char}' at index {self.index}." |
| 118 | +class UsernameTooShort(Exception): |
| 119 | + def __str__(self): |
| 120 | + return "Username is too short. It must be at least 3 characters long." |
| 121 | + |
| 122 | +class UsernameTooLong(Exception): |
| 123 | + def __str__(self): |
| 124 | + return "Username is too long. It must be at most 16 characters long." |
| 125 | + |
| 126 | + |
| 127 | +class PasswordMissingCharacter(Exception): |
| 128 | + def __init__(self, password, missing_char): |
| 129 | + self.password = password |
| 130 | + self.missing_char = missing_char |
| 131 | + |
| 132 | + def __str__(self): |
| 133 | + return f"The password '{self.password}' is missing the required character '{self.missing_char}'." |
| 134 | +class PasswordTooShort(Exception): |
| 135 | + def __str__(self): |
| 136 | + return "Password is too short. It must be at least 8 characters long." |
| 137 | + |
| 138 | +class PasswordTooLong(Exception): |
| 139 | + def __str__(self): |
| 140 | + return "Password is too long. It must be at most 40 characters long." |
| 141 | + |
| 142 | +def check_input(username, password): |
| 143 | + if any(c.isalpha() for c in username): |
| 144 | + raise UsernameContainsIllegalCharacter(username, c, username.index(c)) |
| 145 | + |
| 146 | + if not any(c.isdigit() for c in password): |
| 147 | + raise PasswordMissingCharacter(password, 'a digit') |
| 148 | + elif len(username) < 3: |
| 149 | + raise UsernameTooShort |
| 150 | + elif len(username) > 16: |
| 151 | + raise UsernameTooLong |
| 152 | + elif not any(c.isupper() for c in password) or not any(c.islower() for c in password) or not any(c.isdigit() for c in password) or not any(c in string.punctuation for c in password): |
| 153 | + raise PasswordMissingCharacter |
| 154 | + elif len(password) < 8: |
| 155 | + raise PasswordTooShort |
| 156 | + elif len(password) > 40: |
| 157 | + raise PasswordTooLong |
| 158 | + else: |
| 159 | + print("OK") |
| 160 | + |
| 161 | +def main(): |
| 162 | + while True: |
| 163 | + username = input("Enter a username: ") |
| 164 | + password = input("Enter a password: ") |
| 165 | + try: |
| 166 | + check_input(username, password) |
| 167 | + break |
| 168 | + except Exception as e: |
| 169 | + print(e) |
| 170 | + |
| 171 | +main() |
0 commit comments