首页 > 解决方案 > 创建基本的 Python 函数来测试字符串密码是否满足长度要求并包含特殊字符和数字

问题描述

我是 Python 的初学者(使用 python3),刚刚学习编写基本函数。我正在尝试解决以下问题:编写一个将字符串作为输入的函数,并返回该字符串是否为有效密码(返回值为 True)(返回值为 False)。一个字符串是一个有效的密码,如果它:

  1. 包含至少 1 个介于 0 和 9 之间的数字,并且
  2. 包含列表中的至少 1 个字符 ['$','#','@','.','!','?','<','>'] 和
  3. 最小长度至少为 6 个字符。

正如你在下面看到的,我写了三个非常基本的函数,分别解决了上面的三个条件。我知道有更高级的功能,例如正则表达式可以解决这个问题,但我想坚持基础。

我想创建第四个函数,它通过这些函数运行密码列表,然后确定字符串是否有效。我无法正确创建此功能,非常感谢一些帮助。谢谢!

    passwords_lst = ['ilikeplums!','plum2020','a2b3?','applesaretasty','plum!','plum5','apple','1234','!p1umsareblue'] #list of passwords for testing

    #function for checking if password contains special chars
    def check_specialchar(pwd):
        spec_char_lst=['$','#','@','.','!','?','<','>']
        result=any(elem in spec_char_lst for elem in pwd)
        if result is True:
            print("Has special charactrs")
        else: 
            print("No special characters")
        return pwd
    for password in passwords_lst: #testing with actual list
        print(check_specialchar(password))

    #function for checking if length password>6
    def check_length(pwd):
        if len(pwd)>=6:     
            print("all ok")
        else:
            print("length is less than 6")
        return pwd
    for password in passwords_lst:  #testing with actual list
        print(check_length(password))


    #function for checking if password has at least one digit
    def checkif_digit(pwd):
        digit_count=0
        for char in pwd:
            if char.isdigit():
                digit_count=digit_count+1 #print("Has digits")
        print(digit_count)
            #else:
                #print("No special characters")
        if digit_count>0:
            print("Has at least one digit")
        return pwd
    for password in passwords_lst:  #testing with actual list

        print(checkif_digit(password))

#function for calling all three tests/functions created above and checking if passwords meet all three conditions
def check_pwd(pwd):
    for element in pwd:

    return element
print(check_pwd(passwords_lst))

标签: pythonpython-3.xstringlistfunction

解决方案


  • 鉴于您已经拥有的代码
    • 我为每个函数添加了一个打印语句来指示正在运行的测试
    • True如果测试通过则返回
    • 在您编写现有功能时,我主要留下了它们。
  • 调用其他 3 个函数check_pwd
    • 将列表传递给函数并遍历值以检查它们。
#function for checking if password contains special chars
def check_specialchar(pwd):
    spec_char_lst=['$','#','@','.','!','?','<','>']
    result=any(elem in spec_char_lst for elem in pwd)
    print('Special Characters Check:')
    if result:
        print("Has special charactrs")
        return True
    else: 
        print("No special characters")


#function for checking if length password>6
def check_length(pwd):
    print('Length Check:')
    if len(pwd)>=6:     
        print("all ok")
        return True
    else:
        print("length is less than 6")


#function for checking if password has at least one digit
def checkif_digit(pwd):
    print('Digit Check:')
    digit_count=0
    for char in pwd:
        if char.isdigit():
            digit_count=digit_count+1 
    if digit_count>0:
        print(f"Digit count: {digit_count}")
        return True
    else:
        print("No digits")


def check_pwd(pwd_list):
    for pwd in pwd_list:
        lines = '-'*30
        print(f'{lines}\nTesting: {pwd}')
        s = check_specialchar(pwd)
        l = check_length(pwd)
        d = checkif_digit(pwd)

        print(f'Passed all checks: {all([s, l, d])}')



passwords_lst = ['ilikeplums!','plum2020','a2b3?','applesaretasty','plum!','plum5','apple','1234','!p1umsareblue', '!thistesttopassall3'] #list of passwords for testing

check_pwd(passwords_lst)

运行函数的输出

------------------------------
Testing: ilikeplums!
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum2020
Special Characters Check:
No special characters
Length Check:
all ok
Digit Check:
Digit count: 4
Passed all checks: False
------------------------------
Testing: a2b3?
Special Characters Check:
Has special charactrs
Length Check:
length is less than 6
Digit Check:
Digit count: 2
Passed all checks: False
------------------------------
Testing: applesaretasty
Special Characters Check:
No special characters
Length Check:
all ok
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum!
Special Characters Check:
Has special charactrs
Length Check:
length is less than 6
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum5
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
Digit count: 1
Passed all checks: False
------------------------------
Testing: apple
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: 1234
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
Digit count: 4
Passed all checks: False
------------------------------
Testing: !p1umsareblue
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
Digit count: 1
Passed all checks: True
------------------------------
Testing: !thistesttopassall3
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
Digit count: 1
Passed all checks: True

推荐阅读