Velocity Reviews > Another stupid newbie question

# Another stupid newbie question

Byte
Guest
Posts: n/a

 02-17-2006
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?

-- /usr/bin/byte

Benjamin Niemann
Guest
Posts: n/a

 02-17-2006
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

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/

Ravi Teja
Guest
Posts: n/a

 02-17-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.

Ian Leitch
Guest
Posts: n/a

 02-17-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?
>
> -- /usr/bin/byte
>

if x == mx:
break
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD9iRkefZ4eWAXRGIRAjbLAKCdl5RwFWz7uHSZd38HUj pA4HLGegCfWeP9
9Xw6bHYXhLssLKfG73AOKJo=
=WUrH
-----END PGP SIGNATURE-----

Byte
Guest
Posts: n/a

 02-17-2006
Great, thanks all. Now, how come

if x == mx: print x
break

Dosnt work?
-- /usr/bin/byte

Georg Brandl
Guest
Posts: n/a

 02-17-2006
Byte wrote:
> Great, thanks all. Now, how come
>
> if x == mx: print x
> break
>
> Dosnt work?

If you have a suite with more than one line, you must put it on a new line.

if x == mx:
print x
break

Perhaps it would be good for you to work through the Python Tutorial.

Georg

Byte
Guest
Posts: n/a

 02-17-2006
Great, thanks all (the point of x = 2 was that i dont understand
exactly what True is on while True

-- /usr/bin/byte

Byte
Guest
Posts: n/a

 02-17-2006
Ahhh, Thanks

-- /usr/bin/byte

Steven D'Aprano
Guest
Posts: n/a

 02-18-2006
On Fri, 17 Feb 2006 11:51:08 -0800, Byte wrote:

> Great, thanks all. Now, how come
>
> if x == mx: print x
> break
>
> Dosnt work?

Do yourself a HUGE favour and read this before posting any more questions
to comp.lang.python. Trust me, you really will thank us.

http://www.catb.org/~esr/faqs/smart-questions.html

--
Steven.

Steve Holden
Guest
Posts: n/a

 02-18-2006
Steven D'Aprano wrote:
> On Fri, 17 Feb 2006 11:51:08 -0800, Byte wrote:
>
>
>>Great, thanks all. Now, how come
>>
>> if x == mx: print x
>> break
>>
>>Dosnt work?

>
>
> Do yourself a HUGE favour and read this before posting any more questions
> to comp.lang.python. Trust me, you really will thank us.
>
> http://www.catb.org/~esr/faqs/smart-questions.html
>
>
>

If he doesn't lambast us I'll chalk it up as a win

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/