zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    原生js可爱糖果数字时间特效
    jQuery绑定事件的四种方式
    jQuery选择器总结
    正则表达式
    this对象
    网页瀑布流效果实现的几种方式
    关于DOM
    SparkSQL读写外部数据源--数据分区
    SparkSQL读写外部数据源-通过jdbc读写mysql数据库
    SparkSQL读写外部数据源-基本操作load和save
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/14525358.html
Copyright © 2011-2022 走看看