#!/usr/bin/env python # -*- coding: utf-8 -*-#1. 密码长度应该大于或等于8位#2. 密码必须包含数字、字母及特殊字符三种组合nums = '0123456789'chars1 = 'abcdefghijklmnopqrstuvwxyz'chars2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''def check_length(pwd): if len(pwd) < 8: print("密码至少需要8位") return False else: return True def check_nums(pwd): for each in pwd: if each in nums: return True print("密码应包含数字") return False def check_chars1(pwd): for each in pwd: if each in chars1: return True print("密码应包含小写字母") return False def check_chars2(pwd): for each in pwd: if each in chars2: return True print("密码应包含大写字母") return False def check_symbols(pwd): for each in pwd: if each in symbols: return True print("密码应包含符号") return False def check_password(passwd): # 判断长度 length = check_length(passwd) # 判断是否包含数字 nums = check_nums(passwd) # 判断是否包含小写字母 chars1 = check_chars1(passwd) # 判断是否包含大写字母 chars2 = check_chars2(passwd) # 判断是否包含特殊字符 symbols = check_symbols(passwd) if all([length, nums, chars1, chars2, symbols]): return True def main(): password = raw_input('请输入需要检查的密码:') if check_password(password): print("恭喜你通过了强密码检测") if __name__ == '__main__': main()
用正则表达式实现
#!/usr/bin/env python # -*- coding: utf-8 -*-import reimport pyperclip"""请先将要检测的密码复制到剪切板中再运行程序密码必须八位或以上,同时包含数字、字母大小写和符号"""def check_password(passwd): if r1.search(passwd) == None: print('密码应至少为8位') return False elif r2.search(passwd) == None: print('密码应包含数字') return False elif r3.search(passwd) == None: print('密码应同时包含字母大小写') return False elif r4.search(passwd) == None: print('密码应包含符号') return False else: print('密码检测通过!') return True if __name__ == '__main__': passwd = str(pyperclip.paste()) r1 = re.compile(r'.{8,}') r2 = re.compile(r'\d+') r3 = re.compile(r'[A-Z][a-z]') r4 = re.compile(r'([^a-z0-9A-Z])+') check_password(passwd)