python3学习--随机生成密码、随机生成彩票号

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

1、写一个函数:函数的功能是生成一批密码,存到文件里面
def gen_password(num):
#num 代表生成多少条密码。num代表循环多少次
输入1 生成1条密码
输入2 生成2条密码
pass

2、密码复杂度要求:
(1)长度在8~16位之间
(2)密码必须包括大写字母、小写字母、数字、特殊字符-->随机生成的密码从哪里取? 先和大写字母取交集,然后和小写字母取交集,再与数字取交集,再与特殊字符去交集。
(3)密码不能重复。
3、生成的密码保存到文件里面。(只要保证每次生成的不重复就可以了,不需要保证文件中的密码均不重复)

# Day5-作业1
import string
import random

FILENAME = 'gen_pwd.txt'


def write_file_content(gen_pwd):
    with open(FILENAME, 'a+', encoding='utf-8') as fw:
        fw.seek(0)  # 最新生成的密码写在文件最前面
        fw.write(gen_pwd + '\n')


def gen_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) < num:
        pwd = set(random.sample(l1, random.choice(l2)))
        pwd2 = ''
        s = ''
        # 判断是否包括大写字母、小写字母、数字、特殊字符
        if pwd.intersection(string.ascii_uppercase) and pwd.intersection(string.ascii_lowercase) and pwd.intersection(
                string.digits) and pwd.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 in jihe:
        write_file_content(i)
        count += 1

gen_password(3)

随机生成密码


import random
import string


#
思路1 def gen_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 def gen_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

import string
import random

FILENAME = 'gen_seq.txt'

def write_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']

def gen_seq(num):
    '''
    生成双色球
    :param num: 传入一个变量(作为循环次数)
    :return: True /False
    '''
    red_num2 = ''
    count = 0
    jihe = set()
    while len(jihe) < num:
        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 in jihe:
        write_file_content(i)
        count += 1

gen_seq(3)

 生成双色球

import random


def gen_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)

 

转载于:https://www.cnblogs.com/xm-sunnylin/p/9646698.html

你可能感兴趣的:(python3学习--随机生成密码、随机生成彩票号)