我想弄清楚我的代碼在做什麼錯誤,無法正常工作。即使我在我的代碼中使用小寫,大寫或數字,它仍會打印出聲明,說我沒有。請幫忙!Python - 字符串,列表(密碼檢查器)
# function to check if the password is a valid one
def validatePassword(userPassword):
# if the user has a lowercase character, then the password
# meets validation requirements
lowercase = "abcdefghijklmnopqrstuvwxyz"
if lowercase in userPassword:
hasLowercase = True
else:
hasLowercase = False
print("A lowercase letter is required. ")
# if the user has an uppercase character, then the password
# meets validation requirements
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if uppercase in userPassword:
hasUppercase = True
else:
hasUppercase = False
print("An uppercase letter is required. ")
# if the user has a digit, then the password
# meets validation requirements
digits = "1234567890"
if digits in userPassword:
hasDigits = True
else:
hasDigits = False
print("A digit is required. ")
# if user enters a space, then the password
# does NOT meet validation requirements
whitespace = " "
if whitespace in userPassword:
hasWhitespace = True
print("Password must not contain a space.")
else:
hasWhitespace = False
# the user has successfully met all validation requirements
if ((hasLowercase == True) and (hasUppercase == True) and (hasDigits == True) and (hasWhitespace == False)):
return True
# the user is inputting passwords
def main():
print("Please input passwords one-by-one for validation. ")
print("Enter END after you've entered your last password. ")
passwordList = [""]
while ("END" not in passwordList):
passwordList.append(str(input("")))
del passwordList[-1]
for x in range(1,(len(passwordList))):
userPassword = passwordList[x]
# user has come up with a good password
if (validatePassword(userPassword) == True):
print("Password " + userPassword + " is good ")
# user has come up with a bad password
else:
print("Password " + userPassword + " is bad ")
main()
請檢查'in'的工作方式。當你在word中說「if」asd時:'python檢查'asd'是否整個都在'word'中。單獨不是'a'或's'或'd'。 – Lafexlos
我應該在每個單獨的字母之間使用逗號還是使其成爲列表? –