Python Forum
Syntax "for" loop, "and", ".isupper()", ".islower", ".isnum()"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax "for" loop, "and", ".isupper()", ".islower", ".isnum()"
#1
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 ',  ))
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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')
Reply
#4
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()
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,075 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,727 Dec-18-2018, 04:12 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020