• random模块练习


    2、写一个产生双色球号码的程序,输入几就产生多少条
    1、每次产生的里面不能有重复的
    2、产生的号码要 1 2 3 4 5 6 7
    1-32
    1-17
    01 02 03 04 05 06 07
     
    第一种使用list写法 
    import random

    number = input('number:').strip()
    if number.isdigit() and int(number)>0:
    l = [] 这行解决重复
    while True:
    red = random.sample(range(1,34),6)
    red.sort()
    blue = random.sample(range(1,18),1)
    result = red + blue
    result = [ str(ball).zfill(2) for ball in result]
    seq = ' '.join(result)
    if seq not in l:
    l.append(seq)
    print('生成的双色球号码是:红球:%s 蓝球是:%s'%(' '.join(result[:6]),result[-1]))
    with open('seq.txt', 'w', encoding='utf-8') as fw:
    fw.writelines([line+' ' for line in l])#writelines解决写换行问题
    if int(number) == len(l): 这行解决个数一致和循环的结束 否则一直是死循环
    break
    第二种使用集合写法

    import random
     
    reds = [ str(ball).zfill(2) for ball in range(1,34)]
    blues = [ str(ball).zfill(2) for ball in range(1,18)]
     
     
    def seq():
    number = input('number:').strip()
    if number.isdigit() and int(number)>0:
    l = set()
    while int(number)!=len(l):
    red = random.sample(reds,6)
    red.sort()
    blue = random.sample(blues,1)
    result = red + blue
    seq = ' '.join(result)+' '
    l.add(seq)
    print('生成的双色球号码是:红球:%s 蓝球是:%s'%(' '.join(result[:6]),result[-1]))
     
    with open('seq.txt','w',encoding='utf-8') as fw:
    fw.writelines(l)
     
     
    seq()
  • 相关阅读:
    JavaScript数组升降序排列、最大值、最小值等
    css3箭头
    隐藏显示
    最后一个 last-of-type
    jquery函数封装
    为什么要使用rem
    Git的使用--如何将本地项目上传到Github
    jQuery判断是否选中
    数组索引赋值
    HTML中input和button设置同样高度却不能等高的原因
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/14525358.html
Copyright © 2020-2023  润新知