Another stupid newbie question

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

  1. Byte

    Byte Guest

    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
     
    Byte, Feb 17, 2006
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Byte

    Ravi Teja Guest

    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
    #3
  4. Byte

    Ian Leitch Guest

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

    iD8DBQFD9iRkefZ4eWAXRGIRAjbLAKCdl5RwFWz7uHSZd38HUjpA4HLGegCfWeP9
    9Xw6bHYXhLssLKfG73AOKJo=
    =WUrH
    -----END PGP SIGNATURE-----
     
    Ian Leitch, Feb 17, 2006
    #4
  5. Byte

    Byte Guest

    Great, thanks all. Now, how come

    if x == mx: print x
    break

    Dosnt work?
    -- /usr/bin/byte
     
    Byte, Feb 17, 2006
    #5
  6. Byte

    Georg Brandl Guest

    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
    #6
  7. Byte

    Byte Guest

    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
    #7
  8. Byte

    Byte Guest

    Ahhh, Thanks

    -- /usr/bin/byte
     
    Byte, Feb 17, 2006
    #8
  9. 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
    #9
  10. Byte

    Steve Holden Guest

    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
    #10
  11. Byte

    Byte Guest

    "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
     
    Byte, Feb 19, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brandon McCombs
    Replies:
    4
    Views:
    519
    Richard Wheeldon
    Aug 28, 2006
  2. Ed Dana
    Replies:
    35
    Views:
    956
    Rolf Magnus
    Jan 31, 2007
  3. Darren
    Replies:
    1
    Views:
    418
    Martin Honnen
    Mar 13, 2007
  4. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,025
  5. maryellen sniffen

    another newbie stupid question

    maryellen sniffen, Dec 31, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    114
    Scott Bryce
    Dec 31, 2004
Loading...

Share This Page