My problem is to design a Python script which requires the user to input a password, and let Python validate the password is suitable for the conditions or not.
Here are conditions for the password input by users:
- Begin with letters
- at least 6 characters
- only allowed letters, numbers, - and _ in password
If the conditions match, output Yes. Or else, No.
These are what I have tried:
from sys import exit
def check_alpha(input):
alphas = 0
alpha_list = "A B C D E F G H I J K L M N I O P Q R S T U V W X Y Z".split()
for char in input:
if char in alpha_list:
alphas += 1
if alphas > 0:
return True
else:
return False
def check_number(input):
numbers = 0
number_list = "1 2 3 4 5 6 7 8 9 0".split()
for char in input:
if char in number_list:
numbers += 1
if numbers > 0:
return True
else:
return False
def check_special(input):
specials = 0
special_list = "_-"
for char in input:
if char in special_list:
specials += 1
if specials > 0:
return True
else:
return False
def check_len(input):
if len(input) >= 6:
return True
else:
return False
def validate_password(input):
check_dict ={
'alpha':check_alpha(input),
'number':check_number(input),
'special':check_special(input),
'len':check_len(input)
}
if check_alpha(input) & check_number(input) & check_sprcial(input) & check_len(input)
return True
else:
print"No"
while True:
password = raw_input("Enter password:")
print
if validate_password(password):
print("Yes")
else
print("No")
or alternatively:
import re
while True:
user_input = input("Please enter password:")
is_valid = False
if(len(user_input)<6):
print("No")
continue
elif not re.search("[a-z]",user_input):
print("No")
continue
elif not re.search("[0-9]",user_input):
print("No")
continue
elif re.search("[~!@#$%^&*`+=|\;:><,.?/]",user_input):
print("No")
continue
else:
is_valid = True
break
if(is_valid):
print("Yes")