In python, how do you check if a string has both uppercase and lowercase letters

后端 未结 4 621
独厮守ぢ
独厮守ぢ 2021-01-14 17:34

I\'ve looked at the other post similar to my question, Password check- Python 3, except my question involves checking if the password contains both uppercase and lower case

4条回答
  •  鱼传尺愫
    2021-01-14 18:20

    import sys
    
    def Valid_password_mixed_case(password):
        letters = set(password)
        mixed = any(letter.islower() for letter in letters) and any(letter.isupper() for letter in letters)
        if not mixed:
            print("Invalid password: Mixed case characters not detected", file=sys.stderr)
        return mixed
    

    A complete solution:

    import sys
    
    def Get_Password():
        return input("Enter your desired password: ")
    
    def Count_Digits(password):
        return sum(1 for character in password if character.isdigit())
    
    def Valid_password_length(password):
        correct_length = len(password) >= 10
    
        if not correct_length:
            print("Invalid password: too short", file=sys.stderr)
    
        return correct_length
    
    def Valid_password_characters(password):
        correct_characters = password.isalnum()
    
        if not correct_characters:
            print("Invalid password: illegal character detected", file=sys.stderr)
    
        return correct_characters
    
    def Valid_password_numdigit(password):
        sufficient_digits = Count_Digits(password) >= 2
    
        if not sufficient_digits:
            print("Invalid password: Must have at least 2 digits", file=sys.stderr)
    
        return sufficient_digits
    
    def Valid_password_mixed_case(password):
        letters = set(password)
    
        lower = any(letter.islower() for letter in letters)
        upper = any(letter.isupper() for letter in letters)
    
        if not upper:
            print("Invalid password: No uppercase characters detected", file=sys.stderr)
    
        if not lower:
            print("Invalid password: No lowercase characters detected", file=sys.stderr)
    
        return lower and upper
    
    def password_checker():
        password = Get_Password()
        if Valid_password_length(password) and \
            Valid_password_characters(password) and \
            Valid_password_numdigit(password) and \
            Valid_password_mixed_case(password):
    
            print("Congratulations! This password is valid")
    
    password_checker()
    

提交回复
热议问题