如何创建用于创建新的用户名和密码的代码

时间:2019-03-23 16:31:04

标签: python python-3.x

...密码必须满足以下条件:

  • 超过8个字符
  • 至少1个大写和1个小写字母,1个数字和1个特殊符号
  • 不包含用户名
def enterUsername():
    username = input("Enter username: ")
    username = enterUsername()

    def enterPassword():
        LENGTH = 8
        password = input("Enter password: ")
        upCase = False #indicate if password has at least 1 upper
        lowCase = False #indicate if password has at least 1 lower
        digit = False #indicate if password has at least 1 digit
        special = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '_', '=']
        for char in password: #iterate on each character of password (go through every character, for each character in the string)
            if char.isupper(): #if character is in upper, set upCase to 
                upCase = True
            if char.islower(): #if character is in lower, set lowCase to
                lowCase = True
            if char.isdigit(): #if character is a digit, set digit to 
                digit = True
            for spechar in special:
                if char == spechar:
                    special = True
        length = len(password) #get length of password
        strong = upCase and lowCase and digit and special and length > LENGTH  #strong would be true if all the conditions hold
        while True:
            password = input("Enter password: ")
            if not strong:
                print("Your password is weak.")
            else:
                print ("Your password is strong enough.")
                break
        return password

    enterPassword()

1 个答案:

答案 0 :(得分:2)

这是对我来说很好的测试仪:

password = input('Enter your password here : ')
print('\n')

from re import *

lower_match = compile(r'[a-z]').findall(password)  # Finding if the password contains lowecase letters.
upper_match = compile(r'[A-Z]').findall(password)  # Finding if the password contains uppercase letters.
symbol_match = compile(r'[|\"|\'|~|!|@|#|$|%|^|&|*|(|)|_|=|+|\||,|.|/|?|:|;|[|]|{\}|<|>]').findall(
    password)  # Finding if the password contains special characters.
number_match = compile(r'[0-9]').findall(password)  # Finding if the password contains numbers.

if len(password) < 8 or len(lower_match) == 0 or len(upper_match) == 0 or len(symbol_match) == 0 or len(
    number_match) == 0:
    print('Your password is weak ! ')

elif len(password) >= 8:
    print('Your password is strong ! ')

elif len(password) >= 16:
    print('Your password is very strong ! ')