Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Another stupid newbie question

Reply
Thread Tools

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?

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

 
Reply With Quote
 
 
 
 
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/
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
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?
>
> Thanks in advance,
> -- /usr/bin/byte
>


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

iD8DBQFD9iRkefZ4eWAXRGIRAjbLAKCdl5RwFWz7uHSZd38HUj pA4HLGegCfWeP9
9Xw6bHYXhLssLKfG73AOKJo=
=WUrH
-----END PGP SIGNATURE-----
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
Byte
Guest
Posts: n/a
 
      02-17-2006
Ahhh, Thanks

-- /usr/bin/byte

 
Reply With Quote
 
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.


 
Reply With Quote
 
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid, STUPID question! rincewind HTML 25 05-08-2009 01:07 PM
Another Stupid Newbie Question Ed Dana C++ 35 01-31-2007 01:06 AM
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM



Advertisments