0

I am creating a simple code to check if a password is secure. To be secure a password be at least 8 characters and have one special character. My code does not work properly when 8 characters are entered without a special character. How can I fix it?

#get password
password_in = input("Enter a secure password: ")

#check password is secure
special_ch = ['!', '@', '#', '$', '%', '^', '&', '*']

check = any(item in special_ch for item in password_in)

while len(password_in) <8 and check == False: 
    print("Password is not secure") 
    password_in = input("Please enter a secure password: ")
Corsaka
  • 364
  • 3
  • 15

1 Answers1

0

Change and to or:

while len(password_in) <8 or check is False: 
    ...

Alternatively, to achieve the same result, put in the exact conditions you're looking for and negate all of it:

while not(len(password_in) >= 8 and check is True):
    ...

The fact that these two are logically equivalent is an example of DeMorgan's Law.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53