python编程快速上手之第7章实践项目参考答案
2024-10-16 23:20:30
#!/usr/bin/env python3.5
#coding:utf-8
import re # 7.18.1 # 强口令检测
# 写一个函数,使用正则表达式,确保传入的口令字符串是强口令
# 长度不少于8个字符,同时包含大小写,至少有1个数字 pw = input("请输入口令:")
def checkpw(passwd):
plen = len(passwd)
print(plen)
chpw1 = re.compile(r'.*[A-Z]+.*')
chpw2 = re.compile(r'.*[a-z]+.*')
chpw3 = re.compile(r'.*\d{1,}.*')
chresult1 = chpw1.search(passwd)
print("匹配大写字符",chresult1)
chresult2 = chpw2.search(passwd)
print("匹配小写字符",chresult2)
chresult3 = chpw3.search(passwd)
print("匹配至少1个数字",chresult3)
if (plen >= 8) and (chresult1 != None) and (chresult2 != None) and (chresult3 != None):
print("你的密码符合要求")
else:
print("你的密码不符合要求") checkpw(pw) #7.18.2
# 写一个函数,它接受一个字符串,做的事情和strip()一样
# 如果只传入了要去除的字符串,没有其它参数,那么就去除首尾空白字符
# 否则,函数第二个参数指定的字符将从该字符中去除 # 定义函数,传递2个参数:str1将被去除的字符串,str2接受用户给定的原始字串
# 这里要注意:str1有默认值,要注意它的位置。 string = input("请给定一个待处理的原始字串:")
repstr = input("请输入一个将被删除的字串:")
def newstrip(str2,str1=''):
# 定义x,y变量用于向正则中传递,x用于匹配原始字串开头的空白字符,y用于匹配原始字串结尾的空白字符
x = '^\s*'
y = '\s*$'
# 如果用户没有输入将被删除的字串,那么就返回去除头尾空白字符的原始字串,否则返回被去除指定字串的新字串
if str1 == '':
newstr = re.sub(r'%s|%s'%(x,y),'',str2)
print("你没有输入将被去除的字符,默认将去除首尾空白字符如果有的话")
else:
newstr = re.sub(str1,'',str2)
print("字符" + str1 + "将从原始字串中被去除")
return newstr
print("处理后的字串为:")
if repstr in string:
print(newstrip(string,repstr))
else:
print("你输入的字串不在原始字串中,或者不连续")
最新文章
- 百度富文本编辑器UEDITOR
- [转]在Ubuntu 下安装Redis 并使用init 脚本启动
- 一套简单可依赖的Javascript库
- eclipse下部署web工程的两种方式
- 客户端TortoiseSVN的安装及使用方法
- web.xml文件的作用
- LintCode-Word Search II
- 使用struts的模型驱动注意的问题
- iOS开发中常见的语句@synthesize obj = _obj 的意义详解
- openMP编程(上篇)之指令和锁
- Java安全套接字扩展——JSSE
- Ubuntu18.04关机卡死问题
- Android Studio 错误: 非法字符: '\ufeff'
- 撩课-Web大前端每天5道面试题-Day28
- C++ 获取类成员函数地址方法 浅析
- 2款适合HTML做音频和视频的插件
- MySQL 官方文档
- 1095 Cars on Campus
- leetCode(45):Min Stack
- unity文件写入与读取