Another stupid newbie question

B

Byte

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?

Thanks in advance,
-- /usr/bin/byte
 
B

Benjamin Niemann

Byte said:
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
 
R

Ravi Teja

Benjamin said:
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.
 
I

Ian Leitch

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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?

Thanks in advance,
-- /usr/bin/byte

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

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

Byte

Great, thanks all. Now, how come

if x == mx: print x
break

Dosnt work?
-- /usr/bin/byte
 
G

Georg Brandl

Byte said:
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
 
B

Byte

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

-- /usr/bin/byte
 
B

Byte

"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
more information, just ask. I myself have been on hardware lists/fourms
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
info, if I need it. But questions from people saying 'Please help' etc.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top