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

时间:2018-11-08 17:33:28

标签: python

我正在尝试创建一个用户登录系统程序。我试图确保密码必须至少包含10个字符,但我无法确保密码至少包含两个数字并且仅下划线作为特殊字符。我已经看到一些关于数字的解决方案,但我没有得到,他们很少有至少两位数。

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

这就是我要做的。您可以使用in检查下划线,然后使用正则表达式搜索数字。

import re

test = 'hisod2f_1'

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

if underscore and twonums:
    # your logic

答案 1 :(得分:0)

如果您不想使用正则表达式,则可以添加一些简单的逻辑,如下所示:

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