python实现双色球机选功能

import random
blue = [x for x in range (1,17)]
red = [y for y in range (1,34)]
print "The blue ball is :", random.choice(blue)
for n in range(1,7):
    print "The %d red ball is :" %n, red.pop(random.randint(0, (len(red)-1)))

>>> ================================ RESTART ================================
>>> 
The blue ball is : 3
The 1 red ball is : 17
The 2 red ball is : 22
The 3 red ball is : 26
The 4 red ball is : 30
The 5 red ball is : 27
The 6 red ball is : 25
>>> ================================ RESTART ================================
>>> 
The blue ball is : 9
The 1 red ball is : 32
The 2 red ball is : 23
The 3 red ball is : 15
The 4 red ball is : 8
The 5 red ball is : 17
The 6 red ball is : 9
>>> ================================ RESTART ================================
>>> 
The blue ball is : 6
The 1 red ball is : 15
The 2 red ball is : 16
The 3 red ball is : 11
The 4 red ball is : 23
The 5 red ball is : 10
The 6 red ball is : 27


>>> 

生成过程:red list 依次减少已经取到的红球号数,避免重复抽取到同一个号码;

import random
blue = [x for x in range (1,17)]
red = [y for y in range (1,34)]
print "The blue ball is :", random.choice(blue),'\n'
for n in range(1,7):
    print "The %d red ball are :" %n,red.pop(random.randint(0, (len(red)-1))), '\n'
    print "Now the red ball be left are:",red, '\n'

>>> 
The blue ball is : 2 

The 1 red ball is : 26 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33] 

The 2 red ball is : 25 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 33] 

The 3 red ball is : 14 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 33] 

The 4 red ball is : 29 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 30, 31, 32, 33] 

The 5 red ball is : 8 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 30, 31, 32, 33] 

The 6 red ball is : 31 

Now the red ball be left are: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 30, 32, 33] 

>>> 




posted on 2022-07-05 18:12  我在全球村  阅读(299)  评论(0编辑  收藏  举报