码迷,mamicode.com
首页 > 编程语言 > 详细

python习题-用交集方式产生随机密码

时间:2017-10-20 10:17:00      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:个数   col   随机密码   string   strip   letters   sdi   amp   判断   

# 1、写一个产生密码的程序,
# 输入次数,输入多少次就产生多少条数据,
# 要求密码必须包含大写字母、小写字母和数字,长度8位,不能重复

 1 import  string ,random
 2 num=input(请输入一个数字).strip()
 3 pwds=set()  #定义一个空集合
 4 if num.isdigit():  #判断是否为数字
 5     i=0
 6     while len(pwds)<int(num):
 7         passwd=set(random.sample(string.ascii_letters+string.digits,8))
 8         set1=set(string.ascii_uppercase).intersection(passwd)  #求交集,来判断set1中是否有大写字母
 9         set2 = set(string.ascii_lowercase).intersection(passwd)
10         set3 = set(string.digits).intersection(passwd)
11         if set1 and set2 and set3: #非0既真,判断set1,set2,set3 如果都是真,说明密码中含有了大写字母,小写字母,数字
12             str_passwd=‘‘.join(passwd)+\n#要把产生的密码变成字符串
13             # if str_passwd not in pwds:
14             pwds.add(str_passwd)
15                 # i+=1
16     fw=open(pwds.txt,w)
17     fw.writelines(pwds)
18 else:
19     print(你输入的不是数字)

 

 

python习题-用交集方式产生随机密码

标签:个数   col   随机密码   string   strip   letters   sdi   amp   判断   

原文地址:http://www.cnblogs.com/niuniu2018/p/7696661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!