I have asked this question before, and although the response from the kind StackOverflow users were right, it kinda fails to answer my question, or it’s kinda complicated for a beginner like me, so I’m gonna ask again :).
So here is a code which checks whether a string fits certain criterias or not….
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 password(password: str):
if passwordlength(password):
if not (6 < len(password) < 12):
return False
else:
return True
So this part works fine. no problem with my initial code. However, I’m trying to edit my code so that if a number, character or letter appears 3 times in a row.. for example, AAA or ccc or 111 or ###, the output would be false
Here is my attempt at it, which didn’t work at all…
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 password(password: str):
if passwordlength(password):
if not (6 < len(password) < 12):
return False
for char in password:
if password.count(char) > 3:
return False
return True
return False
What should I change in my code to make sure this works??
Please use the simplest form of coding possible since I’m a beginner and I would like to learn along as I do programming :). Also pls dont use import or collections.iter because I haven’t learnt that yet… I’m learning python online and I haven’t learnt that far yet.. Thank you 🙂
>Solution :
A trivial implementation would be:
def check_repeating_chars(password):
for i in range(2, len(password)):
if password[i] == password[i-1] == password[i-2]:
return False
return True