发布时间:2019-09-11 07:44:06编辑:auto阅读(1468)
偶尔去买双色球,每次购买时都是随机选球,自己平时就用python,当然一直在学习,所以就用python写了一个随机或指定出双色球的脚本。
代码如下:
#!/usr/bin/python # -*- coding=utf-8 -*- # by author San at 2016-03-03 import random import sys def CaiSeQiu(num=1): ''' The function CaiSeQiu is random six numbers for read boll. one boll for blue.Default are six read boll and one blue boll. ''' num=int(num) p = 1 print("随机生成 %s 注球:\n") % num while p <= num: LAN = [] L = [] while len(L) <= 5: L = LAN.append(random.randrange(1, 34)) L = sorted(set(LAN)) BLUE = random.randrange(1, 17) print("红球: %s, 蓝球: %s") % (L,BLUE) p += 1 #########手动输入生成双色球######### def InserQiu(r1,r2,r3,r4,r5,r6,b1): '''Function InserQiu for Insert seven numbers with read bolls, The last one is blue boll range in 1,16;The read's boll range 1,33; ''' RedQius=range(1,34) BludQius=range(1,17) print("红球范围:\n %s") % RedQius print("蓝球范围:\n %s") % BludQius print Input=[r1,r2,r3,r4,r5,r6,b1] RL=sorted(list(set(Input[0:6]))) BL=Input[-1] if BL in BludQius: BLQ=BL for R in RL: if R not in RedQius: print("输入的红球不对,请重新输入") sys.exit() if len(RL) < 6: print("输入的红球有重复,请输入6个没有重复红球~") sys.exit() print("你手选的红球: %s,蓝球: %s") %(RL,BLQ) if __name__ == '__main__': try: num = sys.argv[1] except IndexError: print("请输入×××注数(默认给一注): ") CaiSeQiu(1) sys.exit() else: if not num.isdigit() or sys.argv[1] <= 0: print "出错,请给出正确的票数~ - _ - ~" sys.exit() CaiSeQiu(num)
直接运行脚本生成一注如图:
导入输入指定数
以上是为了练习写出的冗长的代码,哈哈,其实有简单的,来看看
交互模式下:
>>>RedQiu = sorted(random.sample(range(1,34),6)) >>>BlueQiu = random.sample(range(1,17),1) >>>print(RedQiu,BlueQiu) ([4, 8, 11, 22, 31, 32], [13])
赶紧试试,去买一注吧,万一中了呢?呵呵~
上一篇: Python实现计算器功能
下一篇: python实现ssh客户端
47848
46404
37291
34738
29321
25977
24923
19957
19549
18033
5798°
6421°
5936°
5965°
7071°
5920°
5950°
6446°
6408°
7787°