在python中,如何检查字符串是否包含大写和小写字母

时间:2016-10-12 05:26:04

标签: python string

我查看了与我的问题Password check- Python 3类似的其他帖子,但我的问题涉及检查密码是否同时包含大写和小写问题。我的代码如下,但是在执行时,它无法识别密码中的大写和小写字母,它只识别一种类型。如何识别这两种类型呢?还有一种更简单的方法可以让代码检查是否所有这些值都发生而不必为每个步骤创建函数?

def Get_Password():
    return input("Enter your desired password: ")

def Count_Digits(password):
    return sum(character.isdigit() for character in password)

def Valid_password_length(password):
    if len(password) >= 10:
        return ('step1' == True)
    else:
        return ('step1' == False, print("Invalid password: too short"))

def Valid_password_characters(password):
    if password.isalnum():
        return ('step2' == True)
    else:
        return ('step2' == False, print("Invalid password: illegal character detected"))

def Valid_password_numdigit(password):
    if Count_Digits(password) >= 2:
        return ('step3' == True)
    else:
        return ('step3' == False, print("Invalid password: Must have at least 2 digits"))

def Valid_password_lowercase(password):
    for i in (password):
        if i.islower() == True:
            return ('step4' == True)
        else:
            return ('step4' == False, print("Invalid password: No lowercase characters detected"))

def Valid_password_uppercase(password):
    for i in (password):
        if i.isupper() == True:
            return ('step5' == True)
        else:
            return ('step5' == False, print("Invalid password: No uppercase characters detected"))

def password_checker():
    password = Get_Password()
    Valid_password_length(password)
    Valid_password_characters(password)
    Valid_password_numdigit(password)
    Valid_password_lowercase(password)
    Valid_password_uppercase(password)

    if 'step1' and 'step2' and 'step3' and 'step4' and 'step5' == True:
        print("Congratulations! This password is valid")

password_checker()

4 个答案:

答案 0 :(得分:2)

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

完整的解决方案:

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()

答案 1 :(得分:0)

您的问题很容易回答。你将返回return('steps' == True)形式的东西,它总是会返回false。因此,只需使用return Truereturn False替换那些。

假设您修复了上述问题,您的循环也会出错。您只想在循环整个字符串后返回false。考虑密码:ABCd。这包含一个小写字符,但是您的check小写方法将返回false,因为第一个字母是大写的。您希望查看密码中的每个字符,如果这些字符都不是小写的,则返回false。

如果你想要一个更简洁的方法,cdlane's answer是一个很好的pythonic方式。

答案 2 :(得分:0)

回答标记为重复的问题Break line not executing

password = input('Enter your password')
password_letters = list(password)
upper = False
lower = False
name = 'Papa'
surname = 'Johns'

for letter in password_letters:
    if letter.isupper():
        upper = True
    elif letter.islower():
        lower = True

    if upper and lower:
        break

if len(password) <= 5:
    print('password is to short')
elif name.lower() in password.lower() or surname.lower() in password.lower():
    print('You cannot have first or last name in password')
elif not (upper and lower):
    print('You must have both upper and lower case in password')
else:
    print('Password is valid')

答案 3 :(得分:-1)

您可以使用正则表达式:

即。 :

permit_id

希望它有所帮助。