python 实现随机生成包8位包含大写字母、小写字母和数字的密码的程序。
要求:
1用户输入多少次就生成多少条密码,
2要求密码必须同时包含大写字母、小写字母和数字,长度8位,不能重复
代码如下:
import string, random
src_upp = string.ascii_uppercase
src_let = string.ascii_lowercase
src_num = string.digits
lis = []
count = input('请输入次数:').strip() # for 循环实现(产生密码数可能不足)
for i in range(int(count)):
print(i)
# 先随机定义3种类型各自的个数(总数为8)
upp_c = random.randint(1, 6)
low_c = random.randint(1, 8-upp_c - 1)
num_c = 8 - (upp_c + low_c)
# 随机生成密码
password = random.sample(src_upp, upp_c)+random.sample(src_let, low_c)+random.sample(src_num, num_c)
# 打乱列表元素
random.shuffle(password)
# 列表转换为字符串
new_password = ''.join(password)+'\n'
if new_password not in lis:
lis.append(new_password)
with open('password.txt', 'w') as fw:
fw.seek(0)
fw.writelines(lis)
fw.close() # while 循环实现(只有密码不重复才+1)
j=0
while j< int(count):
print(j)
upp_c = random.randint(1, 6)
low_c = random.randint(1, 8 - upp_c - 1)
num_c = 8 - (upp_c + low_c)
# 随机生成密码
password = random.sample(src_upp, upp_c) + random.sample(src_let, low_c) + random.sample(src_num, num_c)
# 打乱列表元素
random.shuffle(password)
# 列表转换为字符串
new_password = ''.join(password) + '\n'
if new_password not in lis:
lis.append(new_password)
j += 1
with open('password.txt', 'w') as fw:
fw.seek(0)
fw.writelines(lis)
fw.close()
# 用集合交集的方法生成密码:

import random,string
num = input('请输入一个数字:').strip()
pwds = set()
if num.isdigit():
while len(pwds)<int(num): # 保证生成条数足够
passwd = set(random.sample(string.ascii_letters+string.digits,))
set1 = set(string.ascii_uppercase).intersection(passwd)
set2 = set(string.ascii_lowercase).intersection(passwd)
set3 = set(string.digits).intersection(passwd)
if set1 and set2 and set3:
str_passwd=''.join(passwd)+'\n'#要把产生的密码变成字符串,因为前面已经给变成集合了
pwds.add(str_passwd)
fw =open('pwds.txt','w')
fw.writelines(pwds)
else:
print('你输入的不是数字')
运行结果如下:

生成密码txt文件内容:

python 几种方法实现随机生成8位同时包含数字、大写字符、小写字符密码的小程序的更多相关文章

  1. Python基础-random模块及随机生成11位手机号

    import random # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围# print(random.randint(1, 20)) # 随机整数,顾头 ...

  2. PHP 小方法之 随机生成几位字符串

    if(! function_exists ('get_rand_string') ) { function get_rand_string($len=6,$format='ALL') { switch ...

  3. [转]Loadrunner随机生成15位数字串

    Loadrunner随机生成15位数字串 PS:http://www.51testing.com/html/43/6343-19789.html 今天看到一个网友的问题,是想生成一个15位的数字串来进 ...

  4. java 随机生成4位随机数

    java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...

  5. js随机生成[n,m)的数字(不包括m)

    Math.random();//随机生成0到1的数字 Math.floor();//取小整 Math.floor(Math.random()*(最大值 - 最小值) + 最小值) 生成2到8的数:Ma ...

  6. 快速排序算法的实现 && 随机生成区间里的数 && O(n)找第k小 && O(nlogk)找前k大

    思路:固定一个数,把这个数放到合法的位置,然后左边的数都是比它小,右边的数都是比它大 固定权值选的是第一个数,或者一个随机数 因为固定的是左端点,所以一开始需要在右端点开始,找一个小于权值的数,从左端 ...

  7. Java通过UUID随机生成36位、32位唯一识别码(唯一字符串)

    import java.util.UUID; /** * 通过UUID随机生成36位.32位唯一识别码(唯一字符串) * @author [J.H] * */ public class Test { ...

  8. Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)

    Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer   Advanced Installer :Free for 30 da ...

  9. python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值

    三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...

随机推荐

  1. SQL时间格式化 转载备用~

    Sel1 取值后格式化{0:d}小型:如2005-5-6{0:D}大型:如2005年5月6日{0:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中 ...

  2. 7-qt随机数qrand

    QT生成随机数和C语言差距不大,C语言用srand()和rand(),QT是用Qsrand()和qrand(): QT生成随机数的格式是: qsrand(QTime(0,0,0).secsTo(QTi ...

  3. js this pointer 指针

    this JavaScript的函数内部如果调用了this,那么这个this到底指向谁? 答案是,视情况而定! 如果以对象的方法形式调用,比如xiaoming.age(),该函数的this指向被调用的 ...

  4. js取当前页面名称

    // 取当前页面名称(不带后缀名)    function pageName()    {        var a = location.href;        var b = a.split(& ...

  5. Vim基础教程

    一.简介 世界上只有三种编辑器,EMACS.VIM和其它. 我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: * ...

  6. sublime的坑

  7. mupdf编译snprintf冲突问题

    mupdf-1.6-source\thirdparty\jbig2dec\config_win32.h //#  define snprintf _snprintf

  8. MySql 几个小技巧

    分页查看: 在 mysql 环境下,执行命令: pager more,之后的结果分屏了. 简明扼要地查看表结构: describe table_name

  9. 让IIS 7 如同IIS 8 第一次请求不变慢

    当我们把网站部署在IIS7或IIS6S的时候,每当IIS或是Application Pool重启后,第一次请求网站反应总是很慢,原因大家都知道(不知道可以参考这个动画说明ASP.NET网页第一个Req ...

  10. 20. Dog,Man's Best Friend 狗,人类最好的朋友

    . Dog,Man's Best Friend 狗,人类最好的朋友 ①The dogs has always been considered man's best friend.Always note ...