Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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密码要求程序_Python - Fatal编程技术网

Python密码要求程序

Python密码要求程序,python,Python,有人能解释一下为什么我的程序没有返回“无效”或“无效”吗?这个程序应该根据大写、小写、数字和特殊的要求来检查输入的密码是否有效。谢谢您的帮助。您的程序不会返回任何内容,因为您没有打印任何内容。除非键入quit,否则您的while将永远循环 def validate(s): global Cap, Low, Num, Spec ''' Checks whether the string s fits the criteria for a valid passwor

有人能解释一下为什么我的程序没有返回“无效”或“无效”吗?这个程序应该根据大写、小写、数字和特殊的要求来检查输入的密码是否有效。谢谢您的帮助。

您的程序不会返回任何内容,因为您没有打印任何内容。除非键入
quit
,否则您的while将永远循环

def validate(s):
    global Cap, Low, Num, Spec
    ''' Checks whether the string s fits the 
        criteria for a valid password.
    ''' 
    capital =['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']
    lowercase = ['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']
    number = ['0','1','2','3','4','5','6','7','8','9']
    special =['@','$','#']

    for i in s:
        if i in capital:
            Cap = True
        else: 
            Cap = False
        if s in lowercase:
            Low = True
        else:
            Low = False
        if s in number:
            Num = True
        else:
            Num = False
        if s in special:
            Spec = True
        else:
            Spec = False

    if Cap and Low and Num and Spec is True:
        return ('valid')
    else:
        return ('not valid')



def main():
    ''' The program driver. '''

    # set cmd to anything except quit()
    cmd = ''

    # process the user commands
    while  cmd != 'quit':
        cmd = input('> ')
        password = cmd 
        validate(password)


main()
如果您使用的是python<3.x,请使用
raw\u input
。如果
$python script.py
不起作用,请尝试
$python-i script.py

希望这有帮助

  • 不要使用全局变量。如果。。。函数内部的else
  • for
    循环之前,将四个变量中的每一个初始化为
    False
    。然后,仅当满足条件时才将其设置为True

  • 您可以缩短字符列表。或者使用比较,如
    if'A'调用此函数的位置?另外,请修正您的缩进,如前所述,
    validate
    是一个不做任何事情的函数,您从全局范围返回,这似乎是错误的。您不断为每个字母重新定义所有内容。只需在循环之前将每个变量定义为
    False
    ,然后在循环中删除所有
    else
    块。此外,在
    if Cap and Low and Num.
    中,您不需要在结尾处使用
    是否为True
    。此外,变量可能不应该是
    全局的
    ,除非一个单词的传递意味着所有后续单词的传递……只是一个小注释,在后面的每三个if语句中,它应该是“i in”,而不是“s in”这不会执行我猜他们要进行的验证检查(要求密码至少包含四个类中的每个类的一个字符)。啊,我很抱歉错过了那个,我会更新。:)谢谢你指出这一点。
    def main():
        cmd = ''
        while  cmd != 'quit':
            cmd = raw_input('> ')
            isValid = validate(cmd)
            print isValid
            if isValid == 'valid':
                return
    
    def validate(s):
      """ Checks whether the string s fits the 
      criteria for a valid password. Requires one of each
      of the following: lowercase, uppercase, digit and special char
      """ 
      special = '@$#'
    
      Cap,Low,Num,Spec = False,False,False,False
      for i in s:
        if i.isupper():
          Cap = True
        elif i.islower():
          Low = True
        elif i.isdigit():
          Num = True
        elif i in special:
          Spec = True
    
      if Cap and Low and Num and Spec:
        return 'valid'
      else:
        return 'not valid'
    
    p = input("Password?")
    print (validate(p))