使用 for 循环验证密码:Python
Password Validation with for loop: Python
我是 Python 的新手,我正在尝试创建一个程序来使用 for 循环验证密码(我已经使用 while 循环编写了一个程序。但是它无法正常工作。
就像我说的,我对 Python 和代码编写很陌生。我发现循环遍历字符串(输入密码)的值并解决如何处理异常(例如,不包含某些字符)特别困难这是我编写的代码。
import string
import re
print('At least 1 letter between [a-z] and 1 letter between [A-Z]. \nAt least 1 number between [0-9]. \nAt least 1 character from [$#@]. \nMinimum length 6 characters. \nMaximum length 16 characters. \n')
def validate():
password = input('Enter your password: \n')
if (len(password) < 8):
print('Password must be at least 8 characters long')
elif (len(password) >= 16):
print('Password must be no more than 16 characteres long')
for i in password:
if (i.find(string.ascii_lowercase)):
print('At least one character must be in the range [a-z]')
elif (i.find(string.ascii_uppercase)):
print('At least one character must be in the range [A-Z]')
elif (i.find(string.digits)):
print('At least one character must be in range[0-9]')
elif (re.search('[@,#,$]', password) is None):
print('At least one of these characters (@ - # -$) must be included')
else:
print('your password is good:')
break
validate()
试试这个:
import re
if re.search("[a-z]+", password) is None:
print('At least one character must be in the range [a-z]')
if re.search("[A-Z]+", password) is None:
print('At least one character must be in the range [A-Z]')
if re.search("[0-9]+", password) is None:
print('At least one character must be in the range [0-9]')
if re.search("[@#$]+", password) is None:
print('At least one character must be in the range [a@#$]')
我想使用 for 循环作为练习。只是为了练习使用它。
感谢您的建议。我对这一切都很陌生,我犯了一些非常令人震惊的错误。
我仍然想要一些关于如何遍历字符串值和处理该字符串中某些字符出现(或不出现)的指示。
再次感谢。
我是 Python 的新手,我正在尝试创建一个程序来使用 for 循环验证密码(我已经使用 while 循环编写了一个程序。但是它无法正常工作。
就像我说的,我对 Python 和代码编写很陌生。我发现循环遍历字符串(输入密码)的值并解决如何处理异常(例如,不包含某些字符)特别困难这是我编写的代码。
import string
import re
print('At least 1 letter between [a-z] and 1 letter between [A-Z]. \nAt least 1 number between [0-9]. \nAt least 1 character from [$#@]. \nMinimum length 6 characters. \nMaximum length 16 characters. \n')
def validate():
password = input('Enter your password: \n')
if (len(password) < 8):
print('Password must be at least 8 characters long')
elif (len(password) >= 16):
print('Password must be no more than 16 characteres long')
for i in password:
if (i.find(string.ascii_lowercase)):
print('At least one character must be in the range [a-z]')
elif (i.find(string.ascii_uppercase)):
print('At least one character must be in the range [A-Z]')
elif (i.find(string.digits)):
print('At least one character must be in range[0-9]')
elif (re.search('[@,#,$]', password) is None):
print('At least one of these characters (@ - # -$) must be included')
else:
print('your password is good:')
break
validate()
试试这个:
import re
if re.search("[a-z]+", password) is None:
print('At least one character must be in the range [a-z]')
if re.search("[A-Z]+", password) is None:
print('At least one character must be in the range [A-Z]')
if re.search("[0-9]+", password) is None:
print('At least one character must be in the range [0-9]')
if re.search("[@#$]+", password) is None:
print('At least one character must be in the range [a@#$]')
我想使用 for 循环作为练习。只是为了练习使用它。
感谢您的建议。我对这一切都很陌生,我犯了一些非常令人震惊的错误。
我仍然想要一些关于如何遍历字符串值和处理该字符串中某些字符出现(或不出现)的指示。
再次感谢。