我正在尝试创建一个用户登录系统程序。我试图确保密码必须至少有 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') breakelse: password = input('Enter your password') print('Visit www.bitly/p8?. to continue')
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
如果您不想使用正则表达式,可以添加一些简单的逻辑,如下所示:
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')
呼啦一阵风
TA贡献1802条经验 获得超6个赞
这就是我要做的。您可以检查下划线in并使用正则表达式来搜索数字。
import re
test = 'hisod2f_1'
underscore = '_' in test
twonums = len(re.findall(r'\d', test)) >= 2
if underscore and twonums:
# your logic
添加回答
举报
0/150
提交
取消