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
user3885927
  • 3,363
  • 2
  • 22
  • 42
Nikolay
  • 80
  • 1
  • 2
  • 9

4 Answers4

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
TomDillinger
  • 194
  • 7
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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • @mgilson I think he is in a learning stage where typing shorthands is not his primary concern. – ha9u63a7 Nov 10 '14 at 23:03
  • 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)) –  Nov 10 '14 at 23:03
  • @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 Nov 10 '14 at 23:11
  • @mgilson You're right, I'm in a learnink stage. Wy you don't you if statement and ise return? How it works? – Nikolay Nov 11 '14 at 13:23
  • def checkio(data): try: data[9] except IndexError: return False return not (data.isdigit() or data.isalpha() or data.islower() or data.isupper()) – Nikolay Nov 11 '14 at 13:35
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
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].

vikramls
  • 1,802
  • 1
  • 11
  • 15
  • 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 Nov 10 '14 at 23:19
  • @mgilson, didn't work for me on ipython - but worked on a python27 console, hence I didn't suggest keeping the generators. – vikramls Nov 11 '14 at 01:32
  • Great explanations. Thanks to you I become little bit more familiarized with generators ) – Nikolay Nov 11 '14 at 13:18