tips for this exercise?

John Salerno johnjsal at NOSPAMgmail.com
Tue Mar 28 15:19:26 EST 2006


I'm working on another exercise now about generating random numbers for 
the lottery. What I want to do is write a function that picks 5 random 
numbers from 1-53 and returns them. Here's what I have so far:

numbers = range(1, 54)

def genNumbers():
     for x in range(5):
     	fiveNumbers = []
     	number = random.choice(numbers)
     	numbers.remove(number)
     	fiveNumbers = fiveNumbers.append(number)
     	return fiveNumbers

Other than being sort of ugly, this also has the side effect of actually 
editing the original list, which I don't want since I will want to 
generate more than one set of numbers.

Is there a better way to extract a certain number of items from a list 
(so maybe I don't need the for loop)? Is a list even the right type to 
use, since it gets edited in place? Perhaps a set?



More information about the Python-list mailing list