验证Python中的小写和大写Ascii



感谢对这个问题的任何帮助,尝试基于包含小写和大写的Ascii进行密码验证。我有替代解决方案的功能,但我试图确定为什么下面的星号(第7行)下面不按预期工作。由于

#password program
#password subroutine
def password(userInput):
result = "passed"
#check for uppercase using ASCII values
**res = any(ord(ele) != 32 and ord(ele) <= 64 or ord(ele) >= 91 for ele in userInput)
#check if length is less than 8
if len(userInput) < 8:
result = "failed"**
#check if there is a number
elif userInput.isalpha():
result = "failed"
#check if there is an uppercase character using ASCII values    
elif str(res) != "True":
result ="failed"

return result  


#main program
#set condition for continuous loop
passwordChecker = True
check = False
while passwordChecker == True:
print("This is a password checker program","n","Enter your password and this program will check it","n")
print("The criteria are:","n","n")
print("Must have at least 8 characters","n","must have at least 1 UPPERCASE, 1 lowercase, and 1 number")
print("Must be able to to be used more than once","n","Must not crash if any of the above criteria are not met","n") 
print("Enter 'End' to exit this checker")
#get user input
userInput = input("Please enter your password: ")
#quit loop
if userInput == 'End' or userInput == 'end' or userInput == 'END':
passwordChecker = False
break
#output result of password check
passFail = password(userInput)
if passFail == "failed":
print('n',"Your password has failed at least one of the checks",'n','n')
#run the checker again
check = input("Would you like to try another password (Y/N)?").upper()
if check != "Y":
passwordChecker = False
else:
print("Your password has passed all of the checks")
break
print("Thank you for using this program")

你可以利用string:

from string import ascii_lowercase, ascii_uppercase, digits

def password(userInput):
if len(userInput) < 8:
print("Password too short")
return False
for characterSet in (ascii_lowercase, ascii_uppercase, digits):
if not any(c in characterSet for c in userInput):
return False
print("Password ok")
return True

password('Password3')

:

Password ok

相关内容

  • 没有找到相关文章

最新更新