Python学习之:文件操作一
作者:互联网
import logging
logging.basicConfig(level=logging.INFO)
def check_number_exist(password_str):
'''
判断字符串中是否含有数字
'''
has_number = False
for c in password_str:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(password_str):
'''
判断字符串中是否含义字母
'''
has_letter = False
for c in password_str:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
'''
主函数
'''
try_times = 5
while try_times >0:
password = input('请输入密码:')
# 判断密码强度
strength_level = 0
# 规则1:判断密码不能小于8位
if len(password) >=8:
strength_level += 1
else:
logging.info('密码长度要求至少8位!')
# 规则2:判断密码必须包含数字
if check_number_exist(password):
strength_level += 1
else:
logging.info('密码必须包含数字!')
# 规则3:判断密码必须包含字母
if check_letter_exist(password):
strength_level += 1
else:
logging.info('密码必须包含字母!')
# 输入的密码写入文件
f = open('password.txt','a')
f.write('密码:{},强度:{} \n'.format(password,strength_level))
f.close()
if strength_level == 3:
logging.info('恭喜,输入的密码强度合格!')
break
else:
logging.info('密码强度不合格!')
try_times -= 1
if try_times <=0:
logging.info('密码次数过度,密码设置失败!')
if __name__ == '__main__':
main()
标签:info,文件,strength,logging,level,Python,学习,密码,password 来源: https://www.cnblogs.com/Jessie-02/p/12625811.html