Python小程序——强口令检测

Python小程序——强口令检测

描述 :写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的
定义是:长度不少于 8 个字符,同时包含大写和小写字符,至少有一位数字。你可
能需要用多个正则表达式来测试该字符串,以保证它的强度。

四个要求:

  1. 长度不少于 8 个字符
  2. 包含大写字符
  3. 包含小写字符
  4. 至少有一位数字

代码如下:

import re
command = 'hji@eee'#不够长度
command1 = 'dasdassfsd3'#没有大写字符
command2 = 'ASDFGHJ77'#没有小写字符
command3 = 'ASDFGHJsdas'#没有数字
command4 = 'ASDaadf123@'#合格的强口令

def judge(command):
    check = re.compile(r'[a-z]')  #必须有小写
    check1 = re.compile(r'[0-9]')  #必须有数字
    check2 = re.compile(r'[A-Z]+')  #必须有大写
    if len(command) < 8:
        print('Your command is not long enough!')
        return False
    if check.search(command) == None:
        print('Your command does not contain small letter!')
        return False
    if check1.search(command) == None:
        print('Your command does not contain numbers!')
        return False
    if check2.search(command) == None:
        print('Your command does not contain capital letter!')
        return False
    print('Your command is up to standard!')
    return True
judge(command)
judge(command1)
judge(command2)
judge(command3)
judge(command4)

输出如下:

Python小程序——强口令检测_第1张图片
Mission all over.

你可能感兴趣的:(Python,python,字符串)