-2

I was writing A function to check if the user inserted a good string that can be counted as a username , python would return True, Else It would return False .

But the problem Is no matter how long the user inserted String Is , It would always return True , I'd be glad if Someone could Help me out.

# Define a function that will check the Chosen UserName , And see If it meet's the minimum Requirement's.
def isusername(UserName : str):


    UserName_Last_Limit = 13
    UserName_First_limit = 3
    UserName_Lengh = len(UserName)
    UserName_Lengh = UserName_Lengh + 1

    
    if UserName.isdigit():
        return False



    # Define A list of allowed character's that can be used in In the character's Of a username.
    allowed = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , ]


    for character in UserName:

        if UserName_Lengh > UserName_Last_Limit :
            return False
    
    
        elif UserName_Lengh < UserName_First_limit:
            return False


        elif character not in allowed:
            return False
        

        else:
            return True


if isusername("Kh"):
    print("True")
else:
    print("False")
deceze
  • 510,633
  • 85
  • 743
  • 889
KhodeNima
  • 1
  • 3
  • Where is there an "exponent" in this code? – Tim Roberts Jul 24 '23 at 05:40
  • 1
    The PROBLEM is that you cannot say a password is valid until the entire loop has finished. You return True after checking the first character. And your first two tests should happen before the loop. They only need to be checked once. – Tim Roberts Jul 24 '23 at 05:41
  • Your string does not allow the digit '0'. Was that intentional? – Tim Roberts Jul 24 '23 at 05:45
  • You can save yourself a lot of typing by importing the *string* module and making use of some of its very useful attributes. – DarkKnight Jul 24 '23 at 05:56

2 Answers2

1

Here is the proper way to organize your code. The length test happens before the loop, and we only return True if we pass through ALL of the checks.

def isusername(UserName : str):

    UserName_Last_Limit = 13
    UserName_First_limit = 3
    
    if UserName.isdigit():
        return False

    # Define A list of allowed character's that can be used in In the character's Of a username.
    allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"

    if len(UserName) > UserName_Last_Limit :
        return False

    if len(UserName) < UserName_First_limit:
        return False

    for character in UserName:
        if character not in allowed:
            return False

    return True

print(isusername("Kh"))
print(isusername("Khj"))
print(isusername("01234"))
print(isusername("Kh-kk"))
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thanks for your answer , And the time you've putted Mr.Roberts And Pardon me if my questioning Wasn't much "Clear" , I'm a newbie in python and stackoverflow . I wish you greatness in your path , And your day Sir. – KhodeNima Jul 24 '23 at 06:07
-1

you can use list comprehension to simplify code:

def isusername(UserName: str):
    UserName_Last_Limit = 13
    UserName_First_limit = 3
    UserName_Lengh = len(UserName)
    UserName_Lengh = UserName_Lengh + 1

    if UserName.isdigit():
        return False

    # Define A list of allowed character's that can be used in In the character's Of a username.
    allowed = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
               "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
               "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", ]
    if UserName_Lengh < UserName_First_limit or UserName_Lengh > UserName_Last_Limit:
        return False
    if not all([True if c in allowed else False for c in UserName]):
        return False
    return True
Xiaomin Wu
  • 400
  • 1
  • 5