【问题标题】:How do i create a code for creating new username and password如何创建用于创建新用户名和密码的代码
【发布时间】:2019-03-23 16:31:04
【问题描述】:

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

  • 超过 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()

【问题讨论】:

  • 欢迎来到stackoverflow。请找出您的代码存在的问题。

标签: python python-3.x


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2013-10-18
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    相关资源
    最近更新 更多