2

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 
1
  • you want if: else if: else ? Try that. Commented Nov 10, 2014 at 22:59

4 Answers 4

2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

@Taha I don't have enought reputation (
1

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.

5 Comments

@mgilson I think he is in a learning stage where typing shorthands is not his primary concern.
You can do it this way as well :: return (len(array)>1) and (len(array)< 64) and (any(x.isdigit() for x in array)) and (any(l.isupper for l in array)) and (any(l.islower for l in array))
@AaronHall -- I don't think that helps. 'aaa'.isalnum() is True, but OP wants to return False unless there is at least 1 from each class of characters present.
@mgilson You're right, I'm in a learnink stage. Wy you don't you if statement and ise return? How it works?
def checkio(data): try: data[9] except IndexError: return False return not (data.isdigit() or data.isalpha() or data.islower() or data.isupper())
1

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 

Comments

1

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].

3 Comments

huh? No ... any will eat up as much of the generator as is necessary and stop as soon as it finds a truthy value. Using a list makes the short-circuiting behavior go away and is therefore less desireable.
@mgilson, didn't work for me on ipython - but worked on a python27 console, hence I didn't suggest keeping the generators.
Great explanations. Thanks to you I become little bit more familiarized with generators )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.