首页 > 编程语言 > 详细

python编程快速上手之第7章实践项目参考答案

时间:2017-04-09 16:56:43      阅读:410      评论:0      收藏:0      [点我收藏+]
 1 #!/usr/bin/env python3.5
 2 #coding:utf-8
 3 import re
 4 
 5 # 7.18.1
 6 
 7 # 强口令检测
 8 # 写一个函数,使用正则表达式,确保传入的口令字符串是强口令
 9 # 长度不少于8个字符,同时包含大小写,至少有1个数字
10 
11 pw = input("请输入口令:")
12 def checkpw(passwd):
13     plen = len(passwd)
14     print(plen)
15     chpw1 = re.compile(r.*[A-Z]+.*)
16     chpw2 = re.compile(r.*[a-z]+.*)
17     chpw3 = re.compile(r.*\d{1,}.*)
18     chresult1 = chpw1.search(passwd)
19     print("匹配大写字符",chresult1)
20     chresult2 = chpw2.search(passwd)
21     print("匹配小写字符",chresult2)
22     chresult3 = chpw3.search(passwd)
23     print("匹配至少1个数字",chresult3)
24     if (plen >= 8) and (chresult1 != None) and (chresult2 != None) and (chresult3 != None):
25         print("你的密码符合要求")
26     else:
27         print("你的密码不符合要求")
28 
29 checkpw(pw)
30 
31 #7.18.2
32 # 写一个函数,它接受一个字符串,做的事情和strip()一样
33 # 如果只传入了要去除的字符串,没有其它参数,那么就去除首尾空白字符
34 # 否则,函数第二个参数指定的字符将从该字符中去除
35 
36 # 定义函数,传递2个参数:str1将被去除的字符串,str2接受用户给定的原始字串
37 # 这里要注意:str1有默认值,要注意它的位置。
38 
39 def newstrip(str2,str1=‘‘):
40     # 定义x,y变量用于向正则中传递,x用于匹配原始字串开头的空白字符,y用于匹配原始字串结尾的空白字符
41     x = ^\s*
42     y = \s*$
43     # 如果用户没有输入将被删除的字串,那么就返回去除头尾空白字符的原始字串,否则返回被去除指定字串的新字串
44     if str1 == ‘‘:
45         newstr = re.sub(r%s|%s%(x,y),‘‘,str2)
46         print("你没有输入将被去除的字符")
47     else:
48         newstr = re.sub(str1,‘‘,str2)
49         print("字符" + str1 + "将从原始字串中被去除")
50     return newstr
51 string = input("请给定一个待处理的原始字串:")
52 repstr = input("请输入一个将被删除的字串:")
53 print("处理后的字串为:")
54 print(newstrip(string,repstr))

 

python编程快速上手之第7章实践项目参考答案

原文:http://www.cnblogs.com/mfyang/p/6685392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!