Another newbie question from Nathan.

Brian van den Broek bvande at po-box.mcgill.ca
Sat Jul 2 02:54:57 EDT 2005


Nathan Pinno said unto the world upon 02/07/2005 02:25:
> 
>   Hi all.
> 
>   How do I make the computer generate 4 random numbers for the guess? I want
> to know because I'm writing a computer program in Python like the game
> MasterMind.
> 
>   Thanks.
> 
>   -- 
>   Nathan Pinno
>   http://www.npinnowebsite.ca/

Hi Nathan,

do you know how to make a program doing anything 4 times? If so, do 
that :-)

Here's one way:

 >>> import random
 >>> def get_master_mind_guess():
... 	guess = []
... 	for i in range(4):
... 		guess.append(random.random())
... 	return guess
...
 >>> get_master_mind_guess()
[0.21236215888513332, 0.096166729147619479, 0.037667620585694728, 
0.77529759474635485]
 >>>

I doubt you need numbers between 0 and 1, but adjust to suit.

I'd also suggest that you might find the Tutor list of real help. 
<http://mail.python.org/mailman/listinfo/tutor>. Nothing wrong with 
not knowing how to do stuff like this -- few were born knowing, after 
all -- but the Tutor list specializes in answering in a way targeted 
to beginners. It's where I learned much of what I know.

Best,

Brian vdB





More information about the Python-list mailing list