1.字符串检验密码强度密码由六位数组成
设置四种密码强弱形式:
a很弱:只包含数字、小写字母、大写字母、和标点符号中一种;
b一般:包含上述任意两种字符
c较强:包含上述任意三种字符
d很强:包含上述四种字符
import string
def check(pwd):
#密码必须至少包含6个字符
if len(pwd)<6:
return 'weak'
d={
1:'week',2:'blew middle',3:'above middle',4:'strong'} #使用字典设置强弱对应类型
r=[False]*4 #需要判断用户输入字符中包含字符的种类,通过列表来判断
for ch in pwd: #使用for循环遍历pwd字符串
if not r[0] and ch in string.digits: #遍历是否存在数字
r[0]=True
if not r[1] and ch in string.ascii_lowercase: #遍历是否存在小写字母
r[1]=True
if not r[2] and ch in string.ascii_uppercase: #遍历是否存在大写字母
r[2]=True
if not r[3] and ch in ',.|;;?<>': #遍历是否存在指定字符
r[3]=True
return d.get(r.cout(True),'error') #记录r中True的数量,并返回字典中对应的值
n=str(input('请输入你的密码(六位字符):'))
print(check(n))
代码链接:
https://download.youkuaiyun.com/download/qxyloveyy/12184552
2.对文件中字符串的操作
实现以下功能:
a.使用open()函数打开并读取文件中的每一行
b.重新生成一个文件,文件名是在原文件名上添加一个后缀的形式,例如原名Car.py=>Car_news.py
c.在新文件中进行写的操作,通过enumerate()遍历行数,根据字符串比较输出指定内容,例如通过strip()和startwith()函数可以实现对开头字符的判断
d.最后使用close()函数关闭文件
import sys
import re
def checkFormats(lines, desFileName):
fp = open(desFileName, 'w')
for i, line in enumerate(lines):
print('=' * 30)
print('Line:', i + 1)
if line.strip().startswith('#'):
print(' ' * 10 + 'Comments.Pass.')
fp.write(line)
continue
flag = True
# check operator symbols
symbols = [',', '+', '-', '*', '/', '//', '**', '>>', '<<', '+=', '-=', '*=', '/=']
temp_line = line
for symbol in symbols:
pattern = re.compile(r'\s*' + re.escape(symbol) + r'\s*')
temp_line = pattern.split(temp_line)
sep = ' ' + symbol + ' '
temp_line = sep.join(temp_line)
if line != temp_line:
flag = False
print(' ' * 10 + 'You may miss some blank spaces in this line.')
# check import statement
if line.strip().startswith('import'):
if ',' in line:
flag = False
print(' ' * 10 + "You'd better import one module at a time.")
temp_line = line.strip()
modules = temp_line[temp_line.index(' ') + 1:]