码迷,mamicode.com
首页 > 编程语言 > 详细

用python 实现生成双色球小程序

时间:2018-02-02 00:38:31      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:comm   return   .com   class   size   格式   生成   红色   pen   

生成双色球小程序:
#输入n,随机产生n条双色球号码,插入n条数据库
#表结构: seq
CREATE TABLE `seq` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`red` varchar(100) NOT NULL,
`blue` varchar(20) NOT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
##“双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1--33中选择;蓝色球号码从1--16中选择。
#05 09 10 12 17 19 13

 1 import random,pymysql,time
 2 #处理球的号码,如果数字是1-9,则在前面加0,-> 09
 3 def process_num(num):
 4     if num in range(1,10):
 5         num = str(num)
 6         new_num = 0 + num
 7     else:
 8         new_num = str(num)
 9     return  new_num
10 def op_mysql(ball_list):
11     conn = pymysql.connect(host=localhost,user=root,passwd=123456,port=3306,db=sakila,charset=utf8)
12     cur = conn.cursor()
13     sql = "insert into seq(red,blue,date) values(%s,%s,%s)"
14     cur.executemany(sql,ball_list) #批量执行
15     conn.commit()
16     cur.close()
17     conn.close()
18 def generate_ball(count):
19     red_ball = process_num(random.randint(1,34))
20     blue_ball = process_num(random.randint(1,17))
21     red_balls = [process_num(x) for x in range(1,34)] # 返回红球01-33的list
22     blue_balls = [process_num(x) for x in range(1,17)] # 返回蓝球01-16的list
23     ball_list = []
24     for i in range(count):
25         red_num = random.sample(red_balls, 6)  # 随机生成6位红球list
26         blue_num = random.sample(blue_balls, 1)
27         cur_time = time.strftime(%Y-%m-%d %H:%M:%S)#当前时间格式化
28         ball = (,.join(red_num),blue_num, cur_time)# 将红,蓝球,时间加入元祖
29         ball_list.append(ball) # 把每个结果加入list,[(‘25,18,23,28,24,19‘, ‘07‘, ‘2018-01-22 22:05:03‘), (‘16,23,27,07,31,21‘, ‘04‘, ‘2018-01-22 22:05:03‘)]
30     op_mysql(ball_list)
31 
32 if __name__ ==__main__:
33     count = int(input(请输入你想生成的双色球号码数量:).strip())
34     generate_ball(count)

 

用python 实现生成双色球小程序

标签:comm   return   .com   class   size   格式   生成   红色   pen   

原文地址:https://www.cnblogs.com/nancyzhu/p/8401572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!