Reputation: 90
I've spend few hours solving this problem but program doesn't work (syntax error). Cheking an answer for similar question didn't help. What's wrong with the code below? I want to chek if list (password) contains at least one digit, as well as containing one uppercase letter and one lowercase letter in it. Please, provide me with the simplest way, I'm a beginner ...
def checkio(password):
array = list(password)
#for letter in array:
if len(array) < 10 or len(array) > 64:
return False
if (any(l.isdigit() for l in array) and (any(l.isupper( for l in array) and (any(l.islower for l in array):
return True
else:
return False
Upvotes: 2
Views: 115
Reputation: 1822
When you do any(l.isdigit() for l in array)
, you are creating a generator. The generator has to be "consumed" for the value to be correct.
In this case, you are better served by using a list instead. Also the call to array = list(password)
is unnecessary, since strings are iterable in python. Here's how the code should look:
def checkio(password):
if len(password) < 10 or len(password) > 64:
return False
if any([c.isdigit() for c in password]) and any([c.islower() for c in password]) and any([c.isupper() for c in password]):
return True
else:
return False
In this version, the any() function is called on temporary lists created using the [c.func() for c in password]
.
Upvotes: 1
Reputation:
You can do it this way, You are missing some parenthesis, another thing is, you said at least
one digit, the length should be <1
. You don't need also to convert to a list, you can iterate strings
def checkio(password):
if len(password) < 1 or len(password) > 64:
return False
if (any(x.isdigit() for x in password)) and (any(l.isupper for l in password)) and (any(l.islower for l in password)):
return True
else:
return False
print checkio("StackO3f") #True
print checkio("S") #False
print checkio("sssss") #False
Upvotes: 1
Reputation: 194
Your parentheses are very wrong. try this.
def checkio(password):
array = list(password)
#for letter in array:
if len(array) < 10 or len(array) > 64:
return False
if ((any(l.isdigit() for l in array)) and (any(l.isupper() for l in array)) and ((any(l.islower() for l in array)))):
return True
else:
return False
Upvotes: 2
Reputation: 310227
Sometimes these things are easiest to see if you format the code nicely. You're missing some parenthesis:
def checkio(password):
if 10 < len(password) or len(password) > 64:
return False
return (any(l.isdigit() for l in password) and
any(l.isupper() for l in password) and
any(l.islower() for l in password)):
Note, you shouldn't have to construct a list from the password -- python strings are iterable and have a well defined length.
Upvotes: 1