2014-10-31 90 views
-1

我不想找人给我解决方案。 我只是寻求一些帮助,为什么这不起作用。 这也是一点点比其他可用的密码强度问题检查密码的强度

def password(pwd): 
     if len(pwd) >= 10: 
      is_num = False 
      is_low = False 
      is_up = False 
      for ch in pwd: 
       if ch.isdigit(): 
        is_num = True 
       if ch.islower(): 
        is_low = True 
       if ch.isupper(): 
        is_up = True 

if __name__ == '__main__': 
    #These "asserts" using only for self-checking and not necessary for auto-testing 
    assert password(u'A1213pokl') == False, "1st example" 
    assert password(u'bAse730onE4') == True, "2nd example" 
    assert password(u'asasasasasasasaas') == False, "3rd example" 
    assert password(u'QWERTYqwerty') == False, "4th example" 
    assert password(u'123456123456') == False, "5th example" 
    assert password(u'QwErTy911poqqqq') == True, "6th example" 
+1

什么是你的问题? – bereal 2014-10-31 12:21:36

+1

你的'密码'函数不会返回任何东西。它总是会返回None。 – CoryKramer 2014-10-31 12:22:48

+0

你还没有说明你在寻找什么功能。你检查返回值,但没有任何。请描述函数调用的期望结果。 – Mike 2014-10-31 12:23:43

回答

1

你错过了2条return语句,使这项工作不同:

def password(pwd): 
    if len(pwd) >= 10: 
     is_num = False 
     is_low = False 
     is_up = False 
     for ch in pwd: 
      if ch.isdigit(): 
       is_num = True 
      if ch.islower(): 
       is_low = True 
      if ch.isupper(): 
       is_up = True 
     return is_num and is_low and is_up 
    return False 
+0

哇!谢啦!这让我大跌眼镜! – ryeth 2014-10-31 12:26:44

+0

嗨@ryeth如果这个或任何答案已经解决了你的问题,请点击复选标记,考虑[接受它](http://meta.stackexchange.com/q/5234/179419)。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 – 2014-10-31 13:06:11

+0

不成问题。我现在要做。我很抱歉,因为这是我第一次真正使用stackoverflow,我现在会更频繁地使用它。谢谢! – ryeth 2014-10-31 13:32:50

1
def password(pwd): 
     upper = any(ch.isupper() for ch in pwd) 
     lower = any(ch.islower() for ch in pwd) 
     is_dig = any(ch.isdigit() for ch in pwd) 
     return upper and lower and is_dig and len(pwd) > 10 
+0

三线时间解决方案? ( – Kroltan 2014-10-31 12:31:46

+0

)循环3次通过字符串,慢于在一个循环中做所有检查,即使更可读 三线性表示三次线性 线性,本身意味着完成所需的时间与元素的数量成正比在列表中。 仍然,很好的解决方案,更pythonic :) – Kroltan 2014-10-31 12:34:21