Search code examples
pythonif-statementjupyter-notebookpassword-protectionany

The user to use special characters when creating a password


I want the user to use special characters when creating a password.(at least 1) Example:

special_character= list(string.punctuation)
password=input("enter password:")
if any(i not in password for i in special_character):
    print("You should add a special character for creating strong password!")

it will be like this but it doesn't work. Can you help me? Thanks.


Solution

  • Just use all(...), or alternatively, use not any(...) but without the not in the list comprehension (this is better because not gets run less times overall, making it marginally more efficient). This is what the code would look like:

    special_characters = list(string.punctuation)
    password = input("Enter password: ")
    if not any(i in password for i in special_characters):
        print("You should add a special character for creating strong password!")