python双色球

 

# 1、红色球6个,1,33,蓝色球1个,1-16
# 1、先从1,33之间取6个 random.sample([1,33],6)
# 2,再从1-16之间取1个 random.choice([1,16])
# 3、把双色球号码改成 红色球 01 02 03 04 05 06 蓝色球 07 的格式
# 4、读到文件的内容, 判断刚才产生的双色球是否在文件中
# 5、不在就写入
import random

FILE_NAME = 'seq.txt'


def op_file(content=None):
with open(FILE_NAME, 'a+', encoding='utf-8') as fw:
if content:
fw.write(content)
else:
fw.seek(0)
res = fw.read()
return res


def seq(num):
count = 0
while count < num:
b1 = [str(i).zfill(2) for i in range(1, 34)] # 产生一个01,02- 33的list
b2 = [str(i).zfill(2) for i in range(1, 17)] # 产生一个01,02- 33的list
red = random.sample(b1, 6) # 返回是一个list
red.sort() # 排序
blue = random.choice(b2)
red_str = ' '.join(red) # '01 02 03 04 05 06'
result = "红色球:%s 蓝色球:%s\n" % (red_str, blue)
all_ball = op_file() # 获取文件内容
if result not in all_ball:
op_file(result) # 写入
count += 1


def seq2(num):
count = 0
while count < num:
red_str = ' '.join(sorted(random.sample([str(i).zfill(2) for i in range(1, 34)], 6))) # 返回是一个list
blue = random.choice([str(i).zfill(2) for i in range(1, 17)])
result = "红色球:%s 蓝色球:%s\n" % (red_str, blue)
if result not in op_file():
op_file(result) # 写入
count += 1


seq2(20)

posted on 2019-04-27 10:48  dongxl  阅读(477)  评论(0编辑  收藏  举报

导航