[Tutor] Another newbie question from Nathan.
adam.jtm30 at gmail.com
Sun Jul 10 22:54:42 CEST 2005
If I understand your problem correctly all you need to do is use the name of
the function to call it ie:
print "hello world"
would give the output
On 7/10/05, Nathan Pinno <falcon3166 at hotmail.com> wrote:
> Hi all,
> How do I make Python get a def? Is it the "get" function, or something
> else? I need to know so that I can get a def for that computer
> MasterMind(tm) game that I'm writing.
> BTW, I took your advice, and wrote some definitions for my Giant
> Calculator program. Might make the code easier to read, but harder to
> because I have to keep going to the top to read the menu. Not that fun,
> necessary for a smooth program, I guess.
> Nathan Pinno
> "Steven D'Aprano" <steve at REMOVETHIScyber.com.au> wrote in message
> news:pan.2005.07.03.02.30.28.82992 at REMOVETHIScyber.com.au...
> > On Sat, 02 Jul 2005 00:25:00 -0600, Nathan Pinno wrote:
> >> Hi all.
> >> How do I make the computer generate 4 random numbers for the
> guess? I
> >> to know because I'm writing a computer program in Python like the
> >> MasterMind.
> > First you get the computer to generate one random number. Then you
> do it
> > again three more times.
> > If you only need to do it once, you could do it this way:
> > import random # you need this at the top of your program
> > x0 = random.random()
> > x1 = random.random()
> > x2 = random.random()
> > x3 = random.random()
> > But if you need to do it more than once, best to create a function
> > returns four random numbers in one go.
> > def four_random():
> > """Returns a list of four random numbers."""
> > L =  # start with an empty list
> > for i in range(4):
> > L.append(random.random())
> > return L
> > and use it this way:
> > rand_nums = four_random()
> > # rand_nums is a list of four numbers
> > print rand_nums # prints the first random number
> > print rand_nums # prints the last one
> > or like this:
> > alpha, beta, gamma, delta = four_random()
> > # four names for four separate numbers
> > Steven.
> > http://mail.python.org/mailman/listinfo/python-list
> Tutor maillist - Tutor at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor