简体   繁体   中英

How can I print something else in a while loop?

I´m having trouble with this while loop, because when it´sa valid password it still prints 'Invalid Password' and I don´t know how to fix it.

import re

password=input('Ingrese su password')
x=True
while x:
    if len(password)<6 and len(password)>12:
        break
    elif not re.findall('[a-z]',password):
        break
    elif not re.findall('[0-9]', password):
        break
    elif not re.findall('[A-Z]', password):
        break
    elif not re.findall('[$#@]',password):
        break
    else:
        print('Valid password')
        break
print('Invalid Password')

Your condition to set the password valid is incorrect. Change your AND by an OR

I think your error comes from the first if statement.

if len(password)<6 and len(password)>12:

should be:

if len(password)<6 or len(password)>12:

Otherwise you will allways get a invalid password message. Greetings Leuko

In addition to the logical error in the first if statement, you have your Invalid Password phrase printing out regardless of other conditions after your while loop exits.

A more sensible way to construct this would be to create a password valid function that you can check in an if statement.

import re

def valid_password(password):
    """Return True if the password meets all criteria, False otherwise"""
    # Check each criteria, returning False if they are not met.
    if len(password)<6 or len(password)>12:
        return False
    elif not re.findall('[a-z]',password):
        return False
    elif not re.findall('[0-9]', password):
        return False
    elif not re.findall('[A-Z]', password):
        return False
    elif not re.findall('[$#@]',password):
        return False

    # If we made it this far it means our password is good
    return True

  
password = input('Ingrese su password')

while not valid_password(password):
    print('Invalid Password')
    password = input('Ingrese su password')
print('Valid Password')

Maybe this could be a simple password validation function:

def validate_password(passwd): 
      
    SpecialSym =['$', '@', '#', '%'] 
    val = True
      
    if len(passwd) < 6: 
        print('length should be at least 6 characters') 
        val = False
          
    if len(passwd) > 12: 
        print('length should not be greater than 12 characters') 
        val = False
          
    if not any(char.isdigit() for char in passwd): 
        print('Password should have at least one numeral') 
        val = False
          
    if not any(char.isupper() for char in passwd): 
        print('Password should have at least one uppercase letter') 
        val = False
          
    if not any(char.islower() for char in passwd): 
        print('Password should have at least one lowercase letter') 
        val = False
          
    if not any(char in SpecialSym for char in passwd): 
        print('Password should have at least one symbol') 
        val = False
    if val: 
        return val 
  
 
def main(): 
    passwd = 'Mypassword123@'
      
    if (validate_password(passwd)): 
        print("Password is valid") 
    else: 
        print("Invalid Password !!") 
          
        
if __name__ == '__main__': 
    main() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM