2016-12-15 83 views
-4

我正在爲學校製作一個密碼項目,我陷入了一個問題。這裏是心不是正常的代碼:密碼python項目編號

def passwordStrength(password): 
    if password.islower(): 
     print("Your password is weak as it only contains lower case letters") 
    elif password.isupper(): 
     print("Your password is weak as it only contains capital letters") 
    elif password.isnumeric(): 
     print("Your password is weak as it only contains numbers") 
    elif password.islower and password.isupper: 
     print("Your password is medium as it contains no numbers") 
    elif password.islower and password.isnumeric: 
     print("Your password is medium as it contains no uppercases") 
    elif password.isupper and password.isnumeric: 
     print("Your password is medium as it contains no lowercases") 
    elif password.islower and password.isupper and password.isnumeric: 
     print("Your password is strong") 

,但如果我的密碼,如「asasASAS1212」鍵入它說,它不包含數字

+0

'islower'是一個函數,而不是一個屬性(等)。 – Sayse

+0

你不叫'islower'和其他人 –

+2

此外,你的同學建議的重複[question](http://stackoverflow.com/q/41117733/1324033)可能會進一步幫助 – Sayse

回答

3

與您的代碼的第一個問題是,你是不是要求方法本身。實質上,在每個引用islower,isupper和isnumeric之後,您需要放置括號(即())。

但是,更深層次的問題在於您使用這些方法的意圖。函數islower,isupper,isnumeric在語義上不是在語義上分別表示「此字符串具有小寫字母字符」,「此字符串具有大寫字母字符」和「此字符串具有數字字符」。這些函數檢查整個字符串是否由獨佔這樣的字符組成。

因此,如果字符串中有單個數字(例如「asd123」),則islower方法返回false,因爲該字符串中的字符不是小寫字母。

該問題的解決方案並非非常有效,它將逐個檢查字符串中的每個字符。

+0

非常感謝你:) – Cole