2014-10-31 96 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