Another stupid newbie question
Ravi Teja
webraviteja at gmail.com
Fri Feb 17 14:29:40 EST 2006
Benjamin Niemann wrote:
> Byte wrote:
>
> > How can I make the following code:
> >
> > from random import random
> >
> >
> >
> > def stuff(a, mx):
> > x = 2
> > while x == 2:
> > x = random()
> > if x == mx: print x
> > else: print 'No luck,', x
> > x = 2
> >
> > Stop when x == mx?
>
> What's the intention behind setting x = 2 at all?
>
> def stuff(a, mx):
> while True:
> x = random()
> if x == mx: print x
> else: print 'No luck,', x
>
> Should do the same as you're code above.
>
> If stuff will never be called with mx=None, I would suggest using
>
> def stuff(a, mx):
> x = None
> while x != mx:
> x = random()
> if x == mx: print x
> else: print 'No luck,', x
>
> Also note that random() returns a float and it is *very* unlikely that the
> condition x == mx will ever come true
Right! And as for stopping use 'return' after your print statement to
exit.
More information about the Python-list
mailing list