使我的密码生成器只允许某些符号

时间:2017-10-05 09:25:17

标签: python

incorrectPassword= True
while incorrectPassword:
    password = input("type in your password: ")
    if len(password) < 8:
        print("your password must be 8 characters long")
    if len(password) >24:
        print("Your password must be shorter than 24 characters")
    elif not any(i.isdigit() for i in password):
        print("you need a number in your password")
    elif not any(i.isupper() for i in password):
        print("you need a capital letter in your password")
    elif not any(i.islower() for i in password):
        print("you need a lowercase letter in your password")
        incorrectPassword = False

如何根据需要仅允许某些字符(如!, $, %, ^, &, *, (, ), -, _, =+)?

2 个答案:

答案 0 :(得分:0)

你可以简单地使用

elif " " in password:
    #print error

或使用Python的re包来定义密码中允许的字符

答案 1 :(得分:0)

我会使用正则表达式,但要以与其他测试类似的方式进行:

any(i not in '!$%^&*()-_=+' for i in password)
相关问题