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

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


这是我的代码:


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


倚天杖
浏览 145回答 2
2回答

SMILET

如果您不想使用正则表达式,可以添加一些简单的逻辑,如下所示:num_count = 0for character in create_pass:&nbsp; &nbsp; if character.isdigit():&nbsp; &nbsp; &nbsp; &nbsp; num_count += 1if num_count < 2:&nbsp; &nbsp; print('You must have at least 2 numbers in your password')

呼啦一阵风

这就是我要做的。您可以检查下划线in并使用正则表达式来搜索数字。import retest = 'hisod2f_1'underscore = '_' in testtwonums = len(re.findall(r'\d', test)) >= 2if underscore and twonums:&nbsp; &nbsp; # your logic
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python