我正试图找出我的代码有哪些地方做错了,哪些地方不能正常工作。即使我在代码中使用小写、大写或数字,它仍然会打印出声明,说我不需要。请帮助!在
# 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 ()
当您说
if '123' in s
时,您需要检查整个123
字符串是否是子字符串。不是每个角色。在将其更改为:
你的代码会起作用的。 注意,我建议您查看字符串方法},并查看模块
upper
和{re
(正则表达式)。这将帮助您为这个案例编写更好的代码。在编辑:
在具体情况下,请使用:
^{pr2}$这个对你有用吗?在
相关问题 更多 >
编程相关推荐