这个代码就是一个简易的密码检查是否符合要求,代码没问题,就是在此基础上,再定义一个专门实现输入的函数,要求在输入时不仅不显示密码,还要显示对应长度的*,麻烦看一下,给一个修改后的代码,谢谢
import getpass
MIN_LENGTH = 5
MAX_LENGTH = 15
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"
def main():
"""Program to get and check a user's password."""
print("Please enter a valid password")
print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
"characters, and contain:")
print("\t1 or more uppercase characters")
print("\t1 or more lowercase characters")
print("\t1 or more numbers")
if SPECIAL_CHARS_REQUIRED:
print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
password = getpass.getpass("> ")
while not is_valid_password(password):
print("Invalid password!")
password = getpass.getpass("> ")
print("Your {}-character password is valid: {}".format(len(password), password))
def is_valid_password(password):
if len(password) < MIN_LENGTH or len(password) > MAX_LENGTH:
return False
count_lower = 0
count_upper = 0
count_digit = 0
count_special = 0
for char in password:
if char.isdigit():
count_digit += 1
elif char.isupper():
count_upper += 1
elif char.islower():
count_lower += 1
elif char in SPECIAL_CHARACTERS:
count_special += 1
if count_lower == 0 or count_upper == 0 or count_digit == 0:
return False
if SPECIAL_CHARS_REQUIRED and count_special == 0:
return False
return True
main()
import getpass
MIN_LENGTH = 5
MAX_LENGTH = 15
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"
def getInput():
return getpass.getpass("> ")
def main():
"""Program to get and check a user's password."""
print("Please enter a valid password")
print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
"characters, and contain:")
print("\t1 or more uppercase characters")
print("\t1 or more lowercase characters")
print("\t1 or more numbers")
if SPECIAL_CHARS_REQUIRED:
print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
password = getInput()
while not is_valid_password(password):
print("Invalid password!")
password = getInput()
print("Your {}-character password is valid: {}".format(len(password), password))
def is_valid_password(password):
if len(password) < MIN_LENGTH or len(password) > MAX_LENGTH:
return False
count_lower = 0
count_upper = 0
count_digit = 0
count_special = 0
for char in password:
if char.isdigit():
count_digit += 1
elif char.isupper():
count_upper += 1
elif char.islower():
count_lower += 1
elif char in SPECIAL_CHARACTERS:
count_special += 1
if count_lower == 0 or count_upper == 0 or count_digit == 0:
return False
if SPECIAL_CHARS_REQUIRED and count_special == 0:
return False
return True
main()
import stdiomask
MIN_LENGTH = 5
MAX_LENGTH = 15
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"
def main():
"""Program to get and check a user's password."""
print("Please enter a valid password")
print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
"characters, and contain:")
print("\t1 or more uppercase characters")
print("\t1 or more lowercase characters")
print("\t1 or more numbers")
if SPECIAL_CHARS_REQUIRED:
print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
password = stdiomask.getpass()
while not is_valid_password(password):
print("Invalid password!")
password = stdiomask.getpass()
print("Your {}-character password is valid: {}".format(len(password), password))
def is_valid_password(password):
if len(password) < MIN_LENGTH or len(password) > MAX_LENGTH:
return False
count_lower = 0
count_upper = 0
count_digit = 0
count_special = 0
for char in password:
if char.isdigit():
count_digit += 1
elif char.isupper():
count_upper += 1
elif char.islower():
count_lower += 1
elif char in SPECIAL_CHARACTERS:
count_special += 1
if count_lower == 0 or count_upper == 0 or count_digit == 0:
return False
if SPECIAL_CHARS_REQUIRED and count_special == 0:
return False
return True
main()