|
| 1 | +# Have the function UsernameValidation(`str`) take the `str` parameter being passed and determine if the string is a valid username according to the following rules: |
| 2 | + |
| 3 | +# 1. The username is between 4 and 25 characters. |
| 4 | +# 2. It must start with a letter. |
| 5 | +# 3. It can only contain letters, numbers, and the underscore character. |
| 6 | +# 4. It cannot end with an underscore character. |
| 7 | + |
| 8 | +#If the username is valid then your program should return the string `true`, otherwise return the string `false`. |
| 9 | + |
| 10 | +def UsernameValidation(strParam): |
| 11 | + |
| 12 | + # username is between 4 and 25 characters |
| 13 | + if len(strParam) <= 25 and len(strParam) >= 4: |
| 14 | + flag1 = True |
| 15 | + else: |
| 16 | + flag1 = False |
| 17 | + |
| 18 | + # start with a letter |
| 19 | + if str(strParam[0]).isalpha(): |
| 20 | + flag2 = True |
| 21 | + else: |
| 22 | + flag2 = False |
| 23 | + |
| 24 | + # contains only letters, numbers and underscore |
| 25 | + valid_grammar = "abcdefghijklmnopqrstuvwxyz0123456789_" |
| 26 | + |
| 27 | + for char in strParam: |
| 28 | + if str(char).isalpha() == False: |
| 29 | + if char in valid_grammar: |
| 30 | + flag3 = True |
| 31 | + else: |
| 32 | + flag3 = False |
| 33 | + |
| 34 | + else: |
| 35 | + if str.lower(char) in valid_grammar: |
| 36 | + flag3 = True |
| 37 | + else: |
| 38 | + flag3 = False |
| 39 | + |
| 40 | + |
| 41 | + # can't end with an underscore |
| 42 | + if str(strParam[-1]) != '_': |
| 43 | + flag4 = True |
| 44 | + else: |
| 45 | + flag4 = False |
| 46 | + |
| 47 | + final_output = flag1 and flag2 and flag3 and flag4 |
| 48 | + |
| 49 | + # code goes here |
| 50 | + return final_output |
| 51 | + |
| 52 | +# keep this function call here |
| 53 | +TC1 = "aa_" |
| 54 | +TC2 = "u__hello_world123" |
| 55 | + |
| 56 | +print(TC1, UsernameValidation(TC1)) |
| 57 | +print(TC2, UsernameValidation(TC2)) |
0 commit comments