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()
  • 相关阅读:
    4-8 求二叉树高度 (20分)
    汉诺塔的递归和非递归实现
    5-18 银行业务队列简单模拟 (25分)
    ACM 刷题小技巧【转】
    5-21 求前缀表达式的值(25分)
    5-20 表达式转换 (25分)
    约瑟夫环----循环链表问题
    关于埃拉托色尼筛选法的整理(质数问题)
    编码---隐藏在计算机软硬件背后的语言
    内排序和外排序扫盲
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/14525358.html
Copyright © 2011-2022 走看看