Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:字符串验证(密码)_Python_Python 3.x - Fatal编程技术网

Python 3:字符串验证(密码)

Python 3:字符串验证(密码),python,python-3.x,Python,Python 3.x,这些语句单独工作(我创建了单独的helper函数),并将它们编译到一个函数中。我怎样才能让他们一起工作?此外,如果没有模块're',我将如何使该程序运行?它工作,但我得到了其他人在这个网站上。这些是我在本计划中需要的: 必须有一个号码 字符串开头和结尾的字符必须是字母 必须介于10到20个字符之间 一行不能有3个字符 以前的密码无法再次使用 这是我的密码: import re def password_validator (pw_v, prev_p): #pw_v = string tha

这些语句单独工作(我创建了单独的helper函数),并将它们编译到一个函数中。我怎样才能让他们一起工作?此外,如果没有模块're',我将如何使该程序运行?它工作,但我得到了其他人在这个网站上。这些是我在本计划中需要的:

  • 必须有一个号码
  • 字符串开头和结尾的字符必须是字母
  • 必须介于10到20个字符之间
  • 一行不能有3个字符
  • 以前的密码无法再次使用
这是我的密码:

import re 
def password_validator (pw_v, prev_p): #pw_v = string that is to be validated; prev_p = previously used strings in a list

    prev_P = [s]
    # do I use 'while True' instead of returning True statements every time?
    if 10 <= len(s) <=20:
        return True
    elif s[0] and s[-1].isalpha():
        return True 
    elif not re.search('(.)\\1{2}', s): # How else can I write this without using 're'?
        return True
    elif any(digit.isdigit() for digit in s):
        return True
    else: 
        return False
重新导入
def password_validator(pw_v,prev_p):#pw_v=要验证的字符串;prev_p=列表中以前使用的字符串
上一页P=[s]
#我是否每次都使用“while True”而不是返回True语句?

如果10将每个条件结果存储在一个变量中,例如具有正确的长度和数字

将它们结合起来:

has_correct_length = (10 <= len(s) <=20)
has_digit = any(digit.isdigit() for digit in s)
fulfills_restrictions = has_correct_length and has_digit

has_correct_length=(10不测试条件是否满足,测试条件是否不满足:

import re 
def password_validator (pw_v, prev_p): #pw_v = string that is to be validated; prev_p = previously used strings in a list

    prev_P = [s]
    # do I use 'while True' instead of returning True statements every time?
    if not 10 <= len(s) <=20:
        return False
    elif not (s[0] and s[-1].isalpha()):
        return False
    elif re.search('(.)\\1{2}', s): # How else can I write this without using 're'?
        return False
    elif not any(digit.isdigit() for digit in s):
        return False
    else: 
        return True
重新导入
def password_validator(pw_v,prev_p):#pw_v=要验证的字符串;prev_p=列表中以前使用的字符串
上一页P=[s]
#我是否每次都使用“while True”而不是返回True语句?

如果不是10您的代码将检查输入是否只满足其中一个条件

请注意,当您返回时,函数返回并忽略代码的其余部分。考虑到这一事实,您可以使用:

(1) 嵌套
if
s

if 10 <= len(s) <= 20:
    if s[0] and s[-1].isalpha():
        # the rest of the conditions
            return True    # all of the conditions were met
return False    # one of the conditions wasn’t met
关于正则表达式的使用,在我看来,在这种情况下是很优雅的;但是您可以始终切换到一个循环,该循环迭代输入的字符,并结合一个重复字符计数器(这没有那么优雅):


您可以使用或更好地组织代码。请注意,return语句意味着退出函数,因此在本例中,如果任何条件匹配,它将返回True。
if not 10 <= len(s) <= 20:
    return False
if not s[0] and s[-1].isalpha():
    return False 
# the rest of the conditions
def three_identical_characters(input):
    counter = 0
    for i in range(1, len(input)):
        counter += (1 if (input[i] == input[i-1]) else 0)
        if counter == 2:
            return True
    return False