我这个是一个基础密码检查的代码,代码没问题,如果再次基础上再单独定义一个函数,有一个要求是可以实现在输入时不会显示密码而是显示*,麻烦看一下,给一个修改后的代码,谢谢啦
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 = input("> ")
while not is_valid_password(password):
print("Invalid password!")
password = input("> ")
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()
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 = input("> ")
while not is_valid_password(password):
print("Invalid password!")
password = input("> ")
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()
在pycharm命令行不行,要在终端运行才能生效:
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()