Reputation: 75
I am a beginner in Python (using python3), and just learning to write basic functions. I am trying to solve the following problem: Write a function that takes a string as input and it returns whether the string is a valid password (the return value is True) or not (the return value is False). A string is a valid password if it:
As you can see below I wrote three very basic functions which address the three conditions above separately. I understand that there are more advanced functions such as regex that would take care of this, but would like to stick to the basics.
I would like to create a fourth function which runs the list of passwords through this functions and then determines whether the string is valid or not. I am unable to create this function properly and would really appreciate some help. Thanks!
passwords_lst = ['ilikeplums!','plum2020','a2b3?','applesaretasty','plum!','plum5','apple','1234','!p1umsareblue'] #list of passwords for testing
#function for checking if password contains special chars
def check_specialchar(pwd):
spec_char_lst=['$','#','@','.','!','?','<','>']
result=any(elem in spec_char_lst for elem in pwd)
if result is True:
print("Has special charactrs")
else:
print("No special characters")
return pwd
for password in passwords_lst: #testing with actual list
print(check_specialchar(password))
#function for checking if length password>6
def check_length(pwd):
if len(pwd)>=6:
print("all ok")
else:
print("length is less than 6")
return pwd
for password in passwords_lst: #testing with actual list
print(check_length(password))
#function for checking if password has at least one digit
def checkif_digit(pwd):
digit_count=0
for char in pwd:
if char.isdigit():
digit_count=digit_count+1 #print("Has digits")
print(digit_count)
#else:
#print("No special characters")
if digit_count>0:
print("Has at least one digit")
return pwd
for password in passwords_lst: #testing with actual list
print(checkif_digit(password))
#function for calling all three tests/functions created above and checking if passwords meet all three conditions
def check_pwd(pwd):
for element in pwd:
return element
print(check_pwd(passwords_lst))
Upvotes: 1
Views: 1170
Reputation: 1741
Using your script as a starting point, I've modified functions to return either True
or False
depending on whether or not certain conditions are met.
def check_specialchar(pwd):
'''return True if contains special chars, else False'''
spec_char_lst = ["$", "#", "@", ".", "!", "?", "<", ">"]
return any(elem in spec_char_lst for elem in pwd)
def check_length(pwd):
'''return True if len >= 6, else False'''
return len(pwd) >= 6
def checkif_digit(pwd):
'''return True if 1 digit or more, else False'''
return any(char.isdigit() for char in pwd)
def check_pwd(pwds):
"""
return a list containing only valid passwords
"""
res = []
for pwd in pwds:
if check_length(pwd) and check_length(pwd) and checkif_digit(pwd):
res.append(pwd)
return res
Here is an example input and output using passwords_lst
.
>>> check_pwd(passwords_lst)
['plum2020', '!p1umsareblue']
Upvotes: 1
Reputation: 62353
True
if test passedcheck_pwd
#function for checking if password contains special chars
def check_specialchar(pwd):
spec_char_lst=['$','#','@','.','!','?','<','>']
result=any(elem in spec_char_lst for elem in pwd)
print('Special Characters Check:')
if result:
print("Has special charactrs")
return True
else:
print("No special characters")
#function for checking if length password>6
def check_length(pwd):
print('Length Check:')
if len(pwd)>=6:
print("all ok")
return True
else:
print("length is less than 6")
#function for checking if password has at least one digit
def checkif_digit(pwd):
print('Digit Check:')
digit_count=0
for char in pwd:
if char.isdigit():
digit_count=digit_count+1
if digit_count>0:
print(f"Digit count: {digit_count}")
return True
else:
print("No digits")
def check_pwd(pwd_list):
for pwd in pwd_list:
lines = '-'*30
print(f'{lines}\nTesting: {pwd}')
s = check_specialchar(pwd)
l = check_length(pwd)
d = checkif_digit(pwd)
print(f'Passed all checks: {all([s, l, d])}')
passwords_lst = ['ilikeplums!','plum2020','a2b3?','applesaretasty','plum!','plum5','apple','1234','!p1umsareblue', '!thistesttopassall3'] #list of passwords for testing
check_pwd(passwords_lst)
------------------------------
Testing: ilikeplums!
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum2020
Special Characters Check:
No special characters
Length Check:
all ok
Digit Check:
Digit count: 4
Passed all checks: False
------------------------------
Testing: a2b3?
Special Characters Check:
Has special charactrs
Length Check:
length is less than 6
Digit Check:
Digit count: 2
Passed all checks: False
------------------------------
Testing: applesaretasty
Special Characters Check:
No special characters
Length Check:
all ok
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum!
Special Characters Check:
Has special charactrs
Length Check:
length is less than 6
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: plum5
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
Digit count: 1
Passed all checks: False
------------------------------
Testing: apple
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
No digits
Passed all checks: False
------------------------------
Testing: 1234
Special Characters Check:
No special characters
Length Check:
length is less than 6
Digit Check:
Digit count: 4
Passed all checks: False
------------------------------
Testing: !p1umsareblue
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
Digit count: 1
Passed all checks: True
------------------------------
Testing: !thistesttopassall3
Special Characters Check:
Has special charactrs
Length Check:
all ok
Digit Check:
Digit count: 1
Passed all checks: True
Upvotes: 1
Reputation: 27547
You can make each function return a boolean, then use the all()
method to see if each function returned True
for the password:
def check_specialchar(pwd):
if any(c in '$#@.!?<>' for c in pwd):
print("Has special charactrs")
return True
else:
print("No special characters")
def checkif_digit(pwd):
if any(c in "1234567890" for c in pwd):
print('Has at least one digit')
return True
else:
print('Doesn\'t have digits')
def check_length(pwd):
if len(pwd) > 5:
print("length is okay")
return True
else:
print("length is less than 6")
def check_pwd(pwd):
if all([check_specialchar(pwd),checkif_digit(pwd),check_length(pwd)]):
print("Good Password")
else:
print("Bad Password")
Test runs:
Input:
check_pwd("dh756dsdf")
print()
check_pwd("sdfg45?")
Output:
No special characters
Has at least one digit
length is okay
Bad Password
Has special charactrs
Has at least one digit
length is okay
Good Password
Upvotes: 0