疫情之后,我打算用python买彩票!(一)

受到疫情的影响,目前福利彩票还没有开始销售,无聊之下,自己陪自己写个程序模拟购买的过程,等到疫情过去,这就是我的彩票利器了,拿去不谢!
之前写的3D福彩的模拟购买并查看是否中奖的代码,可以参考下面的链接:
https://editor.csdn.net/md/?articleId=104487169
我预想要达到的效果
1.输入购买多少注,自动生成
2.模拟生成中奖号码
3.判断是否中奖,中了多少钱,奖金多少
4.购买任意注,筛选出中奖概率最高的一注
先说下自己的思路:
1.双色球由33个红球,和16个蓝球组成,所以定义了2个列表,存放这2种颜色的球
2.定义一个新列表,从红球中每次取1个,如果不在新列表中,则添加进去,反之继续取,知道取出6个,也就是新列表的长度是6时停止;蓝球同样的思路
3.对红球进行从小到达排列,然后格式化输出
代码如下:

import random
def double_color():
    red=[i for i in range(1,34)]
    blue=[i for i in range(1,17)]
    dc_num=int(input('请输入要购买多少注双色球'))
    print('您购买的双色球是')
    while dc_num>0:
        dc_red = []
        dc_blue = []
        while len(dc_red)<7:
            li_red=red[random.randint(0,32)]
            if not li_red in dc_red:
                dc_red.append(li_red)
        dc_red.sort()
        li_blue=blue[random.randint(0,15)]
        dc_blue.append(li_blue)
        print('红球是:{:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} 蓝球是:{:0>2d}'.format(dc_red[0], dc_red[1], dc_red[2], dc_red[3], dc_red[4], dc_red[5],dc_blue[0]))
        dc_num-=1
    print('这次就要中奖了,哈哈')
double_color()

写完代码之后,发现还可以优化下,比如蓝球其实只有一个,直接用随机函数生成就可以了,但用列表表示的方法,可以用到体彩大乐透的选号中,改下参数即可,优化后的双色球代码如下:

import random
def double_color():
    red=[i for i in range(1,34)]
    dc_num=int(input('请输入要购买多少注双色球'))
    print('您购买的双色球是')
    while dc_num>0:
        dc_red = []
        dc_blue = []
        while len(dc_red)<7:
            li_red=red[random.randint(0,32)]
            if not li_red in dc_red:
                dc_red.append(li_red)
        dc_red.sort()
        blue=random.randint(0,15)
        dc_blue.append(li_blue)
        print('红球是:{:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} 蓝球是:{:0>2d}'.format(dc_red[0], dc_red[1], dc_red[2], dc_red[3], dc_red[4], dc_red[5],blue))
        dc_num-=1
    print('这次就要中奖了,哈哈')
double_color()

修改后大乐透的代码如下:

```python
import random
def double_color():
    red=[i for i in range(1,36)]
    blue=[i for i in range(1,13)]
    dc_num=int(input('请输入要购买多少注大乐透'))
    print('您购买的大乐透是')
    while dc_num>0:
        dc_red = []
        dc_blue = []
        while len(dc_red)<6:
            li_red=red[random.randint(0,34)]
            if not li_red in dc_red:
                dc_red.append(li_red)
        dc_red.sort()
        while len(dc_blue)<3:
            li_blue=blue[random.randint(0,11)]
            if not li_blue in dc_blue:
                dc_blue.append(li_blue)
        dc_blue.sort()
        # li_blue=blue[random.randint(0,15)]
        # dc_blue.append(li_blue)
        print('红球是:{:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} 蓝球是:{:0>2d} {:0>2d} '.format(dc_red[0], dc_red[1], dc_red[2], dc_red[3], dc_red[4],dc_blue[0],dc_blue[1]))
        dc_num-=1
double_color()

这几天抽空继续往下写,也还请各位大神指教,请继续关注吧!


发布了13 篇原创文章 · 获赞 1 · 访问量 193

猜你喜欢

转载自blog.csdn.net/aa12551827/article/details/104789046