zoukankan      html  css  js  c++  java
  • python编程快速上手之第7章实践项目参考答案

     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 string = input("请给定一个待处理的原始字串:")
    40 repstr = input("请输入一个将被删除的字串:")
    41 def newstrip(str2,str1=''):
    42     # 定义x,y变量用于向正则中传递,x用于匹配原始字串开头的空白字符,y用于匹配原始字串结尾的空白字符
    43     x = '^s*'
    44     y = 's*$'
    45     # 如果用户没有输入将被删除的字串,那么就返回去除头尾空白字符的原始字串,否则返回被去除指定字串的新字串
    46     if str1 == '':
    47         newstr = re.sub(r'%s|%s'%(x,y),'',str2)
    48         print("你没有输入将被去除的字符,默认将去除首尾空白字符如果有的话")
    49     else:
    50         newstr = re.sub(str1,'',str2)
    51         print("字符" + str1 + "将从原始字串中被去除")
    52     return newstr
    53 print("处理后的字串为:")
    54 if repstr in string:
    55     print(newstrip(string,repstr))
    56 else:
    57     print("你输入的字串不在原始字串中,或者不连续")
  • 相关阅读:
    halcon 常用算子中文解释说明(随时更新)
    傅里叶变换 fft_generic halcon
    phpStorm使用技巧及快捷键
    js控制文本框只能输入数字 及 常用字符对应ASCII码值
    Delphi 7 里没有加载的控件
    用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
    PHP 把GBK编码转换为UTF8
    Oracle性能优化5-索引的不足
    Oracle性能优化4-索引
    Oracle性能优化3-sql优化一定要等价
  • 原文地址:https://www.cnblogs.com/mfyang/p/6685392.html
Copyright © 2011-2022 走看看