2010年6月20日 星期日

[Question 4] Powerball number generator







Description

To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770. Write a program to generate a set of Powerball numbers by utilizing the choice function in Python's random module.
Input

Ask the user how many sets of Powerball numbers he or she would like.
Output

The program will print each set of Powerball numbers in numeric order.
Sample session

Official (but fruitless) Powerball number generator

How many sets of numbers? 3

Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35



#! /usr/bin/env python
import random

print 'Official (but fruitless) Powerball number generator'

set_number = int(raw_input('How many sets of numbers?'))

while set_number > 0:
set_number = set_number - 1

a = []
while 1 :
random_number=int(random.random()*100)
if (random_number > 0 and random_number < 54 ):
a.append(random_number)
if len(a) == 5 :
break

b = []
while 1 :
random_number=int(random.random()*100)
if (random_number > 0 and random_number < 43 ):
b.append(random_number)
break

# , is very important. It can print the word at one line.
print 'Your numbers:',
# format the print '%2d' keep every one have two space to write in.
while len(a) > 0:
print '%2d'%a.pop(),


print ' Powerball:', b.pop()

Related Posts:

0 意見:

張貼留言