CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/lottery.py
Views: 729
1
"""
2
双色球随机选号程序
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-06
7
"""
8
9
from random import randrange, randint, sample
10
11
12
def display(balls):
13
"""
14
输出列表中的双色球号码
15
"""
16
for index, ball in enumerate(balls):
17
if index == len(balls) - 1:
18
print('|', end=' ')
19
print('%02d' % ball, end=' ')
20
print()
21
22
23
def random_select():
24
"""
25
随机选择一组号码
26
"""
27
red_balls = [x for x in range(1, 34)]
28
selected_balls = []
29
for _ in range(6):
30
index = randrange(len(red_balls))
31
selected_balls.append(red_balls[index])
32
del red_balls[index]
33
# 上面的for循环也可以写成下面这行代码
34
# sample函数是random模块下的函数
35
# selected_balls = sample(red_balls, 6)
36
selected_balls.sort()
37
selected_balls.append(randint(1, 16))
38
return selected_balls
39
40
41
def main():
42
n = int(input('机选几注: '))
43
for _ in range(n):
44
display(random_select())
45
46
47
if __name__ == '__main__':
48
main()
49
50