################随机生成密码#####################

1、写一个函数:函数的功能是生成一批密码,存到文件里面

def gen_password(num):

#num 代表生成多少条密码。num代表循环多少次

输入1 生成1条密码

输入2 生成2条密码

pass

2、密码复杂度要求:

(1)长度在8~16位之间

(2)密码必须包括大写字母、小写字母、数字、特殊字符-->随机生成的密码从哪里取? 先和大写字母取交集,然后和小写字母取交集,再与数字取交集,再与特殊字符去交集。

(3)密码不能重复。

3、生成的密码保存到文件里面。(只要保证每次生成的不重复就可以了,不需要保证文件中的密码均不重复)

#Day5-作业1

importstringimportrandom

FILENAME= 'gen_pwd.txt'

defwrite_file_content(gen_pwd):

with open(FILENAME,'a+', encoding='utf-8') as fw:

fw.seek(0)#最新生成的密码写在文件最前面

fw.write(gen_pwd + '\n')defgen_password(num):'''函数的功能是生成一批密码,存到文件里面

:param num: 传入一个变量(作为循环次数)

:return: True /False'''num=int(num)

count=0

l1= string.ascii_uppercase + string.ascii_lowercase + string.digits +string.punctuation

l2= list(range(8, 17))

jihe=set()while len(jihe)

pwd=set(random.sample(l1, random.choice(l2)))

pwd2= ''s= ''

#判断是否包括大写字母、小写字母、数字、特殊字符

if pwd.intersection(string.ascii_uppercase) and pwd.intersection(string.ascii_lowercase) andpwd.intersection(

string.digits)andpwd.intersection(string.punctuation):#实现{'a','B','3','@'}转化为aB3@。也可以使用for循环

#for p in pwd:

#s += p

#write_file_content(s)

pwd2 =s.join(list(pwd))

jihe.add(pwd2)else:

count-= 1

for i injihe:

write_file_content(i)

count+= 1gen_password(3)

随机生成密码

import random

import string

#思路1

defgen_passwords():'''生成一个函数,不必须传参'''pwd_len= random.randint(8,16)

upper= random.sample(string.ascii_uppercase,1) #choice 返回int,sample返回是list,可以去任意位

lower = random.sample(string.ascii_lowercase,1)

digit= random.sample(string.digits,1)

punctuation= random.sample(string.punctuation,1)

other= random.sample(string.ascii_letters+string.digits+string.punctuation,pwd_len-4)

res= upper+lower+digit+punctuation+other#返回的是list

random.shuffle(res)#打乱顺序

return ''.join(res) #每调用一次函数,就返回一次密码

print('1'gen_passwords())#思路2

defgen_passwords2():'''生成一个函数,不必须传参'''pwd_len= random.randint(8,16)

all_str= string.ascii_letters+string.digits+string.punctuation

res= set(random.sample(all_str,pwd_len))#random.sample 返回list,将list转集合

#用&取交集

if res & set(string.ascii_uppercase) and res & set(string.ascii_lowercase) and res & set(string.ascii_letters) and res & set(string.digits) and res &set(string.ascii_letters):return ''.join(res) #.join是字符串的方法,

return gen_passwords2() #使用递归。要加return(表示第一次函数结束运行)。不加return有时候只返回一个none。

print('2',gen_passwords2())

all_passwords=set()

num= int(input('请输入要生产多少条密码:').strip())while len(all_passwords)!=num:

res= gen_passwords2()+'\n'all_passwords.add(res)

with open('passwords.txt','w',encoding='utf-8') as fw:

fw.writelines(all_passwords)

###################随机生成彩票号##################

2、写一个函数,函数的功能是生成一批双色球号码。

def gen_seq(num):

pass

1、中奖号码由6个红球和一个篮球构成

红球的范围是:1~33

篮球的范围是:1~16

2、产生不能重复

篮球:05 红球:01 03 05 17 18 32

篮球:05 红球:01 03 05 17 18 32

篮球:05 红球:01 03 05 17 18 32

篮球:05 红球:01 03 05 17 18 32

篮球:05 红球:01 03 05 17 18 32

#Day5-作业2

importstringimportrandom

FILENAME= 'gen_seq.txt'

defwrite_file_content(gen_ball):'''定义一个写文件的函数

:param gen_pwd:传一个写入文件的内容

:return:'''with open(FILENAME,'a+',encoding='utf-8') as fw:

fw.seek(0)#最新生成的密码写在文件最前面

fw.write(gen_ball+'\n')

num1= ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32']

num2= ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16']defgen_seq(num):'''生成双色球

:param num: 传入一个变量(作为循环次数)

:return: True /False'''red_num2= ''count=0

jihe=set()while len(jihe)

red_num= random.sample(num1, 6)

blue_num=random.choice(num2)#实现将list=[1,2,3,4,5]转变成 str = 1,2,3,4,5。去掉了括号。

red_num2 = ','.join(map(str,red_num)) #方法一:采用map方法

#for i in red_num: # 方法二:循环list,转为str,采用len()方法来确定切片范围

#red_num2 += str(i) + ','

#red_num3 = red_num2[:len(red_num2) - 1]

#print(red_num2)

#print(type(red_num2))s

print('red',red_num2)print('blue',blue_num)

a= '蓝色球:%s,红色球:%s' %(blue_num, red_num2)

jihe.add(a)else:

count-=1

#遍历jihe内的元素,将元素一个个写入到文件中。该遍历应当写在while循环之外,避免元素重复写入。

#之前for写到while里面,输出效果:1,1,2,1,2,3。其中,,第一次1写入三次,第二次2写入两次,第三次3写入一次

for i injihe:

write_file_content(i)

count+= 1gen_seq(3)

生成双色球

importrandomdefgen_seq():

all_red_ball= [str(i).zfill(2) for i in range(1, 34)]

all_blue_ball= [str(i).zfill(2) for i in range(1, 17)]

blue=random.choice(all_blue_ball)

red= random.sample(all_red_ball, 6)

red= ' '.join(red)return '红球:%s 篮球:%s' %(red, blue)print(gen_seq())

all_seq=set()

num= int(input('请输入要生产多少条双色球:').strip())while len(all_seq) !=num:

res= gen_seq() + '\n'all_seq.add(res)

with open('gen__seq.txt', 'w', encoding='utf-8') as fw:

fw.writelines(all_seq)

python彩票生成_python3学习--随机生成密码、随机生成彩票号相关推荐

  1. 对抗生成网络学习(十三)——conditionalGAN生成自己想要的手写数字(tensorflow实现)

    一.背景 其实我原本是不打算做这个模型,因为conditionalGAN能做的,infoGAN也能做,infoGAN我在之前的文章中写到了:对抗神经网络学习(五)--infoGAN生成宽窄不一,高低各 ...

  2. python试卷生成_Python学习笔记文件读写之生成随机的测试试卷文件

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  3. python随机密码生成10个8位密码_生成8位随机密码脚本

    这里提供三个生成随机密码的脚本,两个是shell脚本,一个是python脚本. 先来看第一个shell脚本: 1 [root@sv7 ~]# vim suiji.sh 脚本内容: #!/bin/bas ...

  4. 对抗生成网络学习(十一)——SAGAN生成更为精细的人脸图像(tensorflow实现)

    一.背景 SAGAN全称为Self-Attention Generative Adversarial Networks,是由Han Zhang等人[1]于18年5月提出的一种模型.文章中作者解释到,传 ...

  5. 对抗生成网络学习(七)——SRGAN生成超分辨率影像(tensorflow实现)

    一.背景 SRGAN(Super-Resolution Generative Adversarial Network)即超分辨率GAN,是Christian Ledig等人于16年9月提出的一种对抗神 ...

  6. python随机生成大写字母_python随机生成大小写字母数字混合密码(仅20行代码)

    用简单的方法生成随机性较大的密码 仅用20行代码随机生成密码 核心思路:利用random模块 random模块随机生成数字,大小写字母,循环次数 while循环+随机生成的循环次数-->随机pl ...

  7. c语言中定义密码为英文字母,请设计 一个密码生成器,要求随机生成4组10位密码(C语言)...

    请设计 一个密码生成器,要求随机生成4组10位密码(密码只能由字母和数字组成),每一组必须包含至少一个大写字母,每组密码不能相同,输出生成的密码. #include #include #include ...

  8. 密码生成器c语言程序,请设计 一个密码生成器,要求随机生成4组10位密码(C语言)...

    请设计 一个密码生成器,要求随机生成4组10位密码(密码只能由字母和数字组成),每一组必须包含至少一个大写字母,每组密码不能相同,输出生成的密码. #include #include #include ...

  9. 随机密码生成python_每日一课 | Python 中生成 0 到 9 之间的随机整数

    很少有Python示例向您展示如何生成0(含)和9(含)之间的随机整数0 1 2 3 4 5 6 7 8 9 1.randrange 1.1生成0到9之间的随机整数 #!/usr/bin/python ...

最新文章

  1. 50位青年科学家获颁1.5亿大奖!3位大咖这样寄语
  2. mysql命令技巧_Mysql命令行技巧汇总
  3. 进程间通信的方式(四):信号量
  4. stm32怎么加载字库_收藏 | STM32单片机超详细学习汇总资料(二)
  5. JDBC(九)DatabaseMetaData 数据库元数据
  6. 98k用计算机图片,98K (HandClap)_谱友园地_中国曲谱网
  7. centos查看yum包所有版本(查看包版本)
  8. VMWare 复制虚拟机系统后,模块“Disk”启动失败
  9. 数据库工作笔记009---linux 导入导出postgresql数据库
  10. 测试人如何开展第一份工作?
  11. Linux下创建指定路径下的文件夹/文件,通过get_option()传递路径
  12. 重拾《 两周自制脚本语言 》- 支持中文标识符
  13. 十七.降维之谱分解和奇异值分解
  14. validation参数检验 - 注解介绍
  15. IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈(转)
  16. Progress ThemeBuilder updated Crack
  17. RTL8812F/RTL8197F修改beacon间隔
  18. 获FDA紧急批准,检测新冠肺炎心血管并发症的AI算法将在梅奥诊所应用
  19. ES大量数据条件检索准确性问题
  20. 某溯源平台:vue生成二维码压缩包下载(二)

热门文章

  1. (杂项笔记)关于电脑网卡冲突的问题
  2. 跑胡子程序开发记录(五)——网络版主体功能完成,界面秀
  3. 关于版权和开源许可证
  4. STL算法——常用查找算法(find、find_if、adjacent_find、binary_search、count、count_if)
  5. 亿级数据的高并发通用搜索引擎架构设计(转-张宴)
  6. 蓝桥杯系统练习:回文数、特殊回文数
  7. 数据脱敏,你会了吗(一)
  8. 混乱之治--敏捷的开始。
  9. Js的执行机制(异步)
  10. Gradle Flavor Dimensions 构建变体