你的語法有很多問題。重新格式化你的代碼後, 考慮這些內置方法。 full list here
>>> password = "mylowercasepassword"
>>> password.islower()
True
>>> password2 = "MYSPRPSSWRD"
>>> password2.islower()
False
>>> password2.isupper()
True
>>> pwd = "MySvp3rStr0ngpw4"
>>> pwd.isalpha()
False
>>> pwd.isalnum()
True
代替使用p.lower()
,p.upper()
和等,其始終將評估true
,的使用如下所示的方法。
您可以重寫您的代碼,但嘗試瞭解更多真正的解決方案。順便說一句,我已經修復了你的代碼的一些部分。
p = raw_input("Enter password")
if len(p) <=6:
# abCd1, Psw44 etc.
print "Your password is weak"
elif len(p) >6 and len(p)<=12:
## Password, pAssphrase etc.
if p.islower():
#password, passphrase
print "Length is ok, but you should use mixed case or whatever else"
elif p.isupper() or p.isdigit():
#PASSWORD, PASSPHRASE (is upper), 712356433 (is digit)
print "Another warning message"
#other elifs and else
elif len(p)>12:
#l0ngPasswordjumped0v3r1azyrabbit
if p.isalpha() or p.isalnum():
#myReallyLongPassword (is alpha), l0ngPasswordjumped0v3r1azyrabbit (is alnum)
print "Really strong"
elif (not p.islower()) and (not p.isuppper()) and (not p.isdigit()):
#ParaPsychOlOGY (is not lower, is not upper, and is not digit)
print "Another strong"
#other elifs and else
#other conditional
您可以先修復代碼的格式嗎? – multivac
'p.lower'和'p.lower()'有什麼區別? –
請參閱[**'str.lower()'**](https://docs.python.org/2/library/stdtypes.html#str.lower)的文檔。 Python中的字符串是不可變的,這意味着你不能改變它們。它以小寫形式返回一個新的字符串。你想檢查整個字符串是否小寫?或數字?等等。 –