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 criteria 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 don't 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 :)
CodePudding user response:
You could check this:
any(char * 3 in password
for char in password)
Or with your style:
for char in password:
if char * 3 in password:
return False
CodePudding user response:
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
CodePudding user response:
Here is a function that will take a string as input and will return true
if any character appears three times in a row, otherwise it will return false
:
def does_char_appear_three_times_in_a_row(input_string):
last_char = ''
count = 0
for c in input_string:
if c == last_char:
count = 1
else:
count = 1
if count == 3:
return True
last_char = c
return False
CodePudding user response:
I came up with the following idea:
qtt = 0
last_char = None
for char in password:
if char == last_char:
qtt = 1
else:
qtt = 0
if qtt == 2:
return False
last_char = char
You walk through each char in the string, then compare it with the last char, if they are the same you count, if not, you have to start to count again, so pass 0 to qtt
. If the qtt
equals to 2, you found 2 last_chars equal to chars, it gives to you 3 sequences of the same char. At last, you change the last_char to the actual char, so in the next time it will be the last_char and the for will give you a new one. I hope a was sufficient didactic.
CodePudding user response:
Your code does not work the way you want because password.count(char)
counts all occurrences of char
. A possible way to do what you want to do is this:
def check_char(password):
for n in range(0, (len(password)-2)):
if (password[n] == password[n 1]) and (password[n] == password[n 2]):
return False
return True
But I will recommend using Regex, instead.
CodePudding user response:
I'd say simplest solution would be something like
inarow = 0
for i in range(1, len(password)):
if password[i] == password[i-1]:
inarow = 1
else:
inarow = 1
if inarow == 3:
return False
return True