简体   繁体   English

密码验证中的数字和特殊字符

[英]Password Verfification for numbers and special characters

I am trying to create a user login system program. 我正在尝试创建一个用户登录系统程序。 I am trying to make sure the password must have at least 10 characters which I have done, but I'm having trouble making sure it has at least two numbers and only underscore as a special character. 我试图确保密码必须至少包含10个字符,但我无法确保密码至少包含两个数字并且仅下划线作为特殊字符。 I have seen some solutions about numbers and I don't get them and they rarely have at least 2 digits. 我已经看到一些关于数字的解决方案,但我没有得到,他们很少有至少两位数。

Here is my code: 这是我的代码:

print("Welcome ")
print('')
print('New users should enter Sign to create an account')
print('')
print('')

username = input('Enter your Username:   ')
if username == 'Sign':
    while True:
        usernames = ['Dave','Alice','Chloe']#A list that stores usernames
        create_user = input('Enter your new username:   ')
        if create_user in usernames:
            print('This user name has been taken .Try again')
            continue
        else:
            break
    usernames.append([create_user])
    while True:
        create_pass = input('Enter your your user password:   ')
        passwords = []#A list thst stores password
        pass_len = len(create_pass)
        if pass_len < 10:
            print('Your password must be at least 10. Try again')
            continue
        else:
            print('')
            print('You are now a verified user.')
            print('Run the application again to re-login.')
            print('Thank You')
            break

else:
    password = input('Enter your password')
    print('Visit www.bitly/p8?. to continue')

This is how I'd do it. 这就是我要做的。 You can check for the underscore with in and use regex to search for the numbers. 您可以使用in检查下划线,然后使用正则表达式搜索数字。

import re

test = 'hisod2f_1'

underscore = '_' in test
twonums = len(re.findall(r'\d', test)) >= 2

if underscore and twonums:
    # your logic

If you're not wanting to use regex, you could add some simple logic like this: 如果您不想使用正则表达式,则可以添加一些简单的逻辑,如下所示:

num_count = 0
for character in create_pass:
    if character.isdigit():
        num_count += 1
if num_count < 2:
    print('You must have at least 2 numbers in your password')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM