我正在研究一个python程序来检查文档中的一系列密码,看它们是否有效。它们必须长度为7个字符,包含至少1个大写字母,1个小写,字母,1个数字,不包含其他字符。出于某种原因,我的for循环在仅检查第一个字符后终止,我无法弄清楚问题是什么。有人可以查看我的代码并帮我解决这个问题吗?
def detectLength(word):
if len(word) >= 7:
return True
else:
return False
def detectSpecial(word):
if word.isalnum:
return True
else:
return False
def detectCapital(word):
for i in range(0, len(word)+1):
if (ord(word[i]) >= 65 and ord(word[i]) <= 90):
return True
else:
return False
def detectLower(word):
for i in range(0, len(word)):
if (ord(word[i]) >= 97 and ord(word[i]) <= 122):
return True
else:
return False
def detectDigit(word):
for i in word:
if ord(i) >= 40 and ord(i) <= 57:
return True
else:
return False
def main():
print(format("Password Attempt", '20s'), format("Validity Result", '17s'), format("Reason", '15s'))
print("======================================================")
word = input()
while word != "ZZZZ":
if detectLength(word) == False:
Validity, Reason = "Invalid", "Length"
elif detectSpecial(word) == False:
Validity, Reason = "Invalid", "Special Char"
elif detectCapital(word) == False:
Validity, Reason = "Invalid", "No Uppercase"
elif detectLower(word) == False:
Validity, Reason = "Invalid", "No Lowercase"
elif detectDigit(word) == False:
Validity, Reason = "Invalid", "No Digits"
else:
Validity, Reason = "Valid", ""
print(format(word, '20s'), format(Validity, '20s'), format(Reason, '14s'))
word = input()
print("======================================================")
main()
答案 0 :(得分:0)
这是因为你构建for循环的方式。在detectCapital
中,如果第一个字符不是大写,则返回False
。如果它是大写的,并且如果循环结束并且没有返回True
,则应返回True
,然后显然所有字母都是小写的,并且应返回False
。像这样:
def detectCapital(word):
for i in range(0, len(word)+1):
if (ord(word[i]) >= 65 and ord(word[i]) <= 90):
return True
return False
detectLower
和detectDigit
也存在同样的问题。