python 几种方法实现随机生成8位同时包含数字、大写字符、小写字符密码的小程序
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位同时包含数字、大写字符、小写字符密码的小程序的更多相关文章
- Python基础-random模块及随机生成11位手机号
import random # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围# print(random.randint(1, 20)) # 随机整数,顾头 ...
- PHP 小方法之 随机生成几位字符串
if(! function_exists ('get_rand_string') ) { function get_rand_string($len=6,$format='ALL') { switch ...
- [转]Loadrunner随机生成15位数字串
Loadrunner随机生成15位数字串 PS:http://www.51testing.com/html/43/6343-19789.html 今天看到一个网友的问题,是想生成一个15位的数字串来进 ...
- java 随机生成4位随机数
java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...
- js随机生成[n,m)的数字(不包括m)
Math.random();//随机生成0到1的数字 Math.floor();//取小整 Math.floor(Math.random()*(最大值 - 最小值) + 最小值) 生成2到8的数:Ma ...
- 快速排序算法的实现 && 随机生成区间里的数 && O(n)找第k小 && O(nlogk)找前k大
思路:固定一个数,把这个数放到合法的位置,然后左边的数都是比它小,右边的数都是比它大 固定权值选的是第一个数,或者一个随机数 因为固定的是左端点,所以一开始需要在右端点开始,找一个小于权值的数,从左端 ...
- Java通过UUID随机生成36位、32位唯一识别码(唯一字符串)
import java.util.UUID; /** * 通过UUID随机生成36位.32位唯一识别码(唯一字符串) * @author [J.H] * */ public class Test { ...
- 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 ...
- python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值
三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...
随机推荐
- 奇偶数判断1(if,else if语句)
public class 奇偶数判断 { public static void main(String [] args){ float s = 9f; //取单浮点型变量s,可为任意值 float h ...
- MyEclipse/eclipse 添加作者、注释、版本、时间等
preferences>>java>>code style>>code templates>>comments>>找到相应的编辑即可
- 使用HttpModule实现网址重写和HttpHandler实现页面静态化冲突的解决办法
使用HttpModule实现网址重写和HttpHandler冲突的解决办法功能描述:1. 用HttpModule做了一个重写URL的功能,实现所有访问html的请求要经过httpModule处理,如果 ...
- c++继承赋值兼容
其实还是不明白,红色部分,,,求解 #include <iostream>#include <time.h>using namespace std; class B0{publ ...
- 【校招面试 之 C/C++】第12题 C++ 重载、重写和重定义
1.成员函数重载特征: a.相同的范围(在同一个类中): b.函数名字相同: c.参数不同(参数个数不同或者参数类型不同,但是返回值不同不能使重载): d.virtual关键字可有可无. 2.重写 ...
- collections之deque【双向队列】与Queue【单向队列】
今天来向大家介绍两个队列,一个是deque,双向队列,另外一个是Queue,单向队列,队列和堆栈不同,队列为先进先出,大家还需要注意一下,双向队列为collections模块中的类,而Queue为qu ...
- redis中multi和pipeline区别以及效率(推荐使用pipeline)
手册得知 pipeline 只是把多个redis指令一起发出去,redis并没有保证这些指定的执行是原子的:multi相当于一个redis的transaction的,保证整个操作的原子性,避免由于中途 ...
- catkin_make 与cmake
http://blog.csdn.net/zyh821351004/article/details/50388429 1. catkin_make 与cmake的关系 程序在cmake编译的流程: ...
- web 批量打印
批量打印,同时打印多个页面,有两种思路: 第一种思路,将所有的页面内容加载到一个页面中,然后再打印.这种打印方式有几个弊端,页面的样式会丢失,页面太多同时加载到一个页面中,数据量太大,响应时间很长,消 ...
- LINQ to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”。
错误原因: bool res1 = S_ROLE_MENU_PURVIEWCODE_Manage.Delete(c => c.MPC_CODE == strs[0]); linq不能写strs[ ...