Python Forum

Full Version: Syntax "for" loop, "and", ".isupper()", ".islower", ".isnum()"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I'm trying to run any code but my Kernel needs to be re-started all the time (I have Windows, is that related?), besides I have syntax problems. If someone can help me, I would really appreciate it!

#3. A website requires a user to input username and password to register. 
#   Write a program to check the validity of password given by user. 
#   Following are the criteria for checking password:
​
#  1. At least 1 letter between [a-z] 
#  2. At least 1 number between [0-9] 
#  3. At least 1 letter between [A-Z]
#  4. At least 1 character from [$#@]
#  5. Minimum length of transaction password: 6 
#  6. Maximum length of transaction password: 12
​
#Hint: In case of input data being supplied to the question, 
#it should be assumed to be a console input.

name=str(input('Name: ', ))
​
password=str(input('Enter a password with: At least 1 capital or lower case letter, 1 number and 1 of these characters: $#@. The password must have between 6 and 12 characters ',  ))
Error:
File "<ipython-input-2-97a716fd38be>", line 6 if not password.isupper() and not password.islower() and not password.isnum() ^ SyntaxError: invalid syntax
characters=list(password)
special_characters=['$', '#','@']
Error:
NameError Traceback (most recent call last) <ipython-input-3-a33fe6ef79e7> in <module> ----> 1 characters=list(password) 2 special_characters=['$', '#','@'] NameError: name 'password' is not defined
if not password.isupper() and not password.islower() and not password.isnum() 
and  character in special_charaters and len(password)>=6 and len(password)<=12
print('Valid password')
else print ('Invalid password')
password=str(input('Enter a password with: At least 1 capital or lower case letter, 1 number and 1 of these characters: $#@. The password must have between 6 and 12 characters ',  ))
The first error is because you need a colon at the end of an if condition. The second error looks to be that you did not define password variable.
I figured it out!



name=str(input('Name: ', ))
​
password=str(input('Enter a password with: At least 1 capital or lower case letter, 1 number and 1 of these characters: $#@. The password must have between 6 and 12 characters ',  ))
characters=list(password)
special_characters=['$', '#','@']

def intersection (characters, special_characters):
    return set(characters).intersection(set(special_characters))



                    
if password.isupper():
    print("Password is not valid")
else:
    if password.islower(): 
        print("Password is not valid")
    else:     
        if password.isnumeric(): 
            print("Password is not valid")
        else:
            if  len(intersection(characters, special_characters))==0:
                print("Password is not valid")
            else:
                if len(password)<6 or len(password)>12:
                    print("Password is not valid")
                else:
                    print('Valid password')
Your code is not working properly.
It says "#....." or "$....." or "@....." are valid passwords, and that´s not true.
So only one check is working and that´s if there is a special character in the password,
all other checks are not working as you assume.

Maybe have a look here: https://docs.python.org/3/library/string.html
string.ascii_lowercase .ascii_uppercase and .digits might be interesting

think about the possibilities of using any():
https://docs.python.org/3/library/functi...ht=any#any

and combine that with using all()
https://docs.python.org/3/library/functi...ht=all#all

btw: input() returns a string, so no need to convert a string to a string using str()
Note that this code:

if password.isupper():
    print("Password is not valid")
else:
    if password.islower(): 
        print("Password is not valid")
    else:     
        if password.isnumeric(): 
            print("Password is not valid")
        else:
            if  len(intersection(characters, special_characters))==0:
                print("Password is not valid")
            else:
                if len(password)<6 or len(password)>12:
                    print("Password is not valid")
                else:
                    print('Valid password')
Is equivalent to this code:

if password.isupper():
    print("Password is not valid")
elif password.islower(): 
    print("Password is not valid")
elif password.isnumeric(): 
    print("Password is not valid")
elif len(intersection(characters, special_characters))==0:
    print("Password is not valid")
elif len(password)<6 or len(password)>12:
    print("Password is not valid")
else:
    print('Valid password')
That is, 'elif' in python is equivalent to 'else if' in many other languages. That's not going to fix the problems ThomasL pointed out, but it's definitely something you should use moving forward.