# Another stupid newbie question

Discussion in 'Python' started by Byte, Feb 17, 2006.

1. ### ByteGuest

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

Byte, Feb 17, 2006

2. ### Benjamin NiemannGuest

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/

Benjamin Niemann, Feb 17, 2006

3. ### Ravi TejaGuest

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.

Ravi Teja, Feb 17, 2006
4. ### Ian LeitchGuest

-----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)

iD8DBQFD9iRkefZ4eWAXRGIRAjbLAKCdl5RwFWz7uHSZd38HUjpA4HLGegCfWeP9
9Xw6bHYXhLssLKfG73AOKJo=
=WUrH
-----END PGP SIGNATURE-----

Ian Leitch, Feb 17, 2006
5. ### ByteGuest

Great, thanks all. Now, how come

if x == mx: print x
break

Dosnt work?
-- /usr/bin/byte

Byte, Feb 17, 2006
6. ### Georg BrandlGuest

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

Georg Brandl, Feb 17, 2006
7. ### ByteGuest

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

-- /usr/bin/byte

Byte, Feb 17, 2006
8. ### ByteGuest

Ahhh, Thanks

-- /usr/bin/byte

Byte, Feb 17, 2006
9. ### Steven D'ApranoGuest

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.

Steven D'Aprano, Feb 18, 2006
10. ### Steve HoldenGuest

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/

Steve Holden, Feb 18, 2006
11. ### ByteGuest

"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 "

I find that webpage highly insulting, and so should you. It is treating
you like a small child, who needs to be told everything. If you need
for quite a while now. I never answer questions that go into too much
detail, I find it off-putting and insulting - I feel it treats me like
some sort of robot, that requires all eventualities to be programed
into it to start answering the questions. I just ask politly for more
is great. They know they are in the wrong, and intend to be humorus.
They dont intend to annoy/offend anybody. And really, I dont need to be
told how to be polite, thank you. As regards to grammer/spelling, what
if sombody is unsure of how to spell something? And did you ever hear
of being in a rush?

-- /usr/bin/byte

Byte, Feb 19, 2006