So I wrote this code originally for passwordlength(password)
to find whether these criterias are met or not...
def passwordlength(password: str):
upper = digit = special = False
for char in password:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and "1234567890" and "abcdefghijklmnopqrstuvwxyz":
upper = True
if char in "1234567890":
digit = True
if char in "$#@%!":
special = True
if upper and digit and special:
return True
return False
and now I created a new function to find whether the length of the string is within 6 to 12 characters AND whether each element in the string appears lesser than 3 times or not.. and here it is
def passwordOK(password: str):
if passwordlength(password)== True:
if len(password) > 6 and len(password) < 12:
return True
for char in password:
if password.count(char) <= 3:
return True
else:
return False
if passwordlength(password) == False:
return False
print(passwordOK(password='aaaa1jd4kjfs$jDhfksfd'))
When I tried to run this, it didn't work at all.. Can someone please help me find the error and correct me? Thank You:)
CodePudding user response:
The following would resemble some workable logic and code:
def passwordlength(password: str):
upper = digit = special = False
for char in password:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" :
upper = True
elif char in "1234567890":
digit = True
elif char in "$#@%!":
special = True
return upper and digit and special
def passwordOK(password: str):
if passwordlength(password):
if not (6 < len(password) < 12):
return False # return False early
for char in password:
if password.count(char) > 3:
return False # again: return False early
return True # only when all chars have been checked, you know it's ok
return False
Of course the naming is still weird. If I were to redo it:
from collections import Counter
from string import ascii_uppercase, digits, punctuation
def characterclass_check(password: str):
for char_class in (ascii_uppercase, digits, punctuation):
if not any(c in char_class for c in password):
return False
return True
def passwordOK(password: str):
if not characterclass_check(password):
return False
if not (6 < len(password) < 12):
return False
return max(Counter(password).values()) <= 3
CodePudding user response:
There are many problems with this code.
First
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and "1234567890" and "abcdefghijklmnopqrstuvwxyz"
will be evaluated as
if (char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") and ("1234567890") and ("abcdefghijklmnopqrstuvwxyz")
thus only checking if the character is uppercase, as the other strings will evaluate to True
simply by having content. The variable name you're setting is upper
, so I'm not sure why those other strings are there to start with - they're not what you're trying to check against.
Second, you unnecessarily call passwordlength(password)
twice when the second one could (and should) be an else
.
Third, you return immediately on checking the password length in passwordOK
, so if it's between 6 and 12 characters you never check for duplicate characters.
Fourth, when you check for duplicate characters you return as soon as you find one that appears less than 4 times, meaning that "aaaaaabbbbbbcdddddd" would pass. I assume this was not your intention.
Fifth, your else
on the for
loop is unnecessary as in your case it will always run if you don't return from within the loop. It's only useful if you have a break
.
Sixth, you have a function named passwordlength
that doesn't actually check the length of the password.
I may have missed other issues.