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. OpenCV(图像处理)—访问像素的三种方法

    方法一:用指针访问像素 #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include < ...

  2. Housewife Wind(边权树链剖分)

    Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS   Memory Limit: 65536K Total Submis ...

  3. TZOJ 3533 黑白图像(广搜)

    描述 输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如图所示的图形有3个八连块. 输入 第1行输入一个正 ...

  4. SqlServer 查询死锁,杀死死锁进程*转载

    原文: -- 查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName fro ...

  5. [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积

    Given an array nums of n integers where n > 1,  return an array output such that output[i] is equ ...

  6. FutureTask详解

    1 基本概念 1.1 Callable与Future Runnable封装一个异步运行的任务,可以把它想象成为一个没有参数和返回值的异步方法.Callable与Runnable类似,但是有返回值.Ca ...

  7. eclipse中导入dtd文件实现xml的自动提示功能

    以mybatis为例 1.mybatis的xml文件头: (1)config文件: <?xml version="1.0" encoding="UTF-8" ...

  8. apache的错误日志分析

  9. Java中通过SimpleDateFormat格式化当前时间:/** 输出格式:20060101010101001**/

    import java.util.*; import java.text.SimpleDateFormat; int y,m,d,h,mi,s,ms; String cur; Calendar cal ...

  10. process概念

    multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...