【问题标题】:Password Verfification for numbers and special characters数字和特殊字符的密码验证
【发布时间】:2018-11-09 01:48:17
【问题描述】:

我正在尝试创建一个用户登录系统程序。我正在尝试确保密码必须至少包含 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')

【问题讨论】:

  • 要求特殊字符是好的。限制允许使用哪些特殊字符是不好的,而且通常是不必要的。您对允许集的约束越多,暴力破解密码所需的工作就越少。

标签: python


【解决方案1】:

如果你不想使用正则表达式,你可以像这样添加一些简单的逻辑:

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

【讨论】:

  • 非常感谢。这对数字非常有用。
【解决方案2】:

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

import re

test = 'hisod2f_1'

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

if underscore and twonums:
    # your logic

【讨论】:

  • 谢谢它的工作,但我将如何只为一个下划线输入做到这一点
  • @daliseiy 您可以使用与正则表达式相同的方式执行此操作,但将 \d 替换为 _
猜你喜欢
  • 2020-10-05
  • 2021-10-12
  • 2011-09-06
  • 1970-01-01
  • 2020-10-16
  • 2016-07-05
  • 1970-01-01
  • 2023-03-12
  • 2013-05-16
相关资源
最近更新 更多