首页 > 解决方案 > 如何创建用于创建新用户名和密码的代码

问题描述

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

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

标签: pythonpython-3.x

解决方案


这是一个适合我的测试仪:

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 ! ')

推荐阅读