zoukankan      html  css  js  c++  java
  • python—模拟生成双色球号

    双色球规则:“双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1--33中不重复选择;蓝色球号码从1--16中选择。

    # -*- coding:UTF-8 -*-
    import random,time

    def process_int(x):
    '''
    这个函数用来把int类型转成字符串
    '''
    x = str(x)
    if len(x)==1:
    #如果是个位数前面加0
    x='0'+x
    return x

    def tickets(num):
    '''
    :num 产生几条
    这个函数是用来随机产生双色球号码的,
    每次把产生的号码保存在当天日期的文件中
    '''
    red_nums = list(map(process_int,range(1,34)))
    #红球,范围在1-33,使用map把每个元素传给process_int转成字符串
    blue_nums = list(map(process_int,range(1,17)))
    #蓝球,范围在1-16
    res_list = []#保存所有的结果,用来写到文件里面
    for i in range(1,num+1):
    red_num = random.sample(red_nums, 6)
    blue_num = random.sample(blue_nums, 1)
    res = red_num+blue_num
    format_str = '第%s个:红球:%s 蓝球:%s'%(i,' , '.join(res[:6]),res[-1])
    res_list.append(format_str+' ')
    print(format_str)
    cur_time = time.strftime('%Y.%m.%d %H_%M_%S')
    with open('%s.txt'%cur_time,'w',encoding='utf-8') as fw:
    fw.writelines(res_list)

    if __name__ =='__main__':
    nums = input('请输入你要产生多少条双色球号码:') #.strip()
    tickets(int(nums))

    函数解释:

    (1)0python range() 函数可创建一个整数列表。

      range(start, stop[, step]) 

      参数说明:

    • start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
    • stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
    • step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

     (2)map() 会根据提供的函数对指定序列做映射。

      map(function, iterable, ...) 

      第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

      参数说明:

    • function -- 函数
    • iterable -- 一个或多个序列

    (3)random.sample(red_nums, 6)      randon下的sample()函数,指从一组数据“red_nums”中,随机抽取6个不重复数值,即不放回抽数。

      参考:https://javywang.blog.csdn.net/article/details/90667570    “Python不重复批量随机抽样 random.sample() 和 numpy.random.choice() 的优缺点”

    (4).strip()

      Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

      脚本中这一点,我给去掉了,因为老是数据类型报错。

    PS:关于缩进和空格的报错问题“IndentationError: unindent does not match any outer indentation level笔记”   这是我用Notepad++按照该链接调整Notepad++设置之后的脚本样子,可供参考。

    生活其实也很简单,喜欢的就去争取、得到的就珍惜、失去的就忘记。
  • 相关阅读:
    unity3d应用内分享(微信、微博等)的实现
    Cocostudio 文章列表
    C++ 文章列表
    Android 文章列表
    js函数节流和函数防抖
    js实现队列-通过闭包方式
    初学js正则
    Android网络图片加载
    利用html5制作正方体,同时实现3D旋转效果
    Python模块——random随机模块
  • 原文地址:https://www.cnblogs.com/Formulate0303/p/14031748.html
Copyright © 2011-2022 走看看