Re: Loop problem while generating a new value with random.randint()

Discussion in 'Python' started by Jean-Michel Pichavant, Feb 15, 2010.

  1. Paulo Repreza wrote:
    > Greetings,
    >
    > I'm having problems with a little script that I'm trying to finish, I
    > don't know if I'm in the right track but I know somebody is going to
    > help me.
    >
    > The script:
    >
    > # Import modules random for function randint
    >
    > import random
    >
    > # Generating a constant.
    >
    > var = 65
    >
    > # Generating a random number.
    > ranum = random.randint(1,100)
    >
    > #Creating while loop. Stops until var == ranum
    > while var != ranum:
    > if var == ranum:
    > print var, 'equals to:', ranum
    > else:
    > print var, 'does not equal to:', ranum
    >
    > ########## End of Script ###########
    >
    >
    > What I'm trying to do is to print the new value that ranum generates
    > if the condition is not met. So far if you run the script it prints
    > the same value over and over again, making in an infinite loop. What
    > can I do in order to print out the new value generated every time the
    > condition is not met?
    >
    > Thanks!
    >
    > Paulo Repreza

    in your script you generate the random number only once, no wonder it
    keep being the same value over & over.

    # Initialize ranum with a random number
    ranum = random.randint(1,100)

    #Creating while loop. Stops until var == ranum
    while var != ranum:
    if var == ranum:
    print var, 'equals to:', ranum
    else:
    print var, 'does not equal to:', ranum
    ranum = random.randint(1,100) # generate a new number, that's the
    missing line in your script

    JM
    Jean-Michel Pichavant, Feb 15, 2010
    #1
    1. Advertising

  2. Jean-Michel Pichavant a écrit :
    > Paulo Repreza wrote:
    >> Greetings,
    >>
    >> I'm having problems with a little script that I'm trying to finish, I
    >> don't know if I'm in the right track but I know somebody is going to
    >> help me.


    (snip - problem already addressed by Jean-Michel...)

    >> while var != ranum:
    >> if var == ranum:


    Note that this branch will never be executed - the condition in the
    while statement make sure var != ranum in the while block.

    >> print var, 'equals to:', ranum
    >> else:
    >> print var, 'does not equal to:', ranum
    >>
    Bruno Desthuilliers, Feb 15, 2010
    #2
    1. Advertising

  3. Jean-Michel Pichavant <> writes:

    > Paulo Repreza wrote:
    >> Greetings,
    >>
    >> I'm having problems with a little script that I'm trying to finish,
    >> I don't know if I'm in the right track but I know somebody is going
    >> to help me.
    >>
    >> The script:
    >>
    >> # Import modules random for function randint
    >>
    >> import random
    >>
    >> # Generating a constant.
    >>
    >> var = 65
    >>
    >> # Generating a random number.
    >> ranum = random.randint(1,100)
    >>
    >> #Creating while loop. Stops until var == ranum
    >> while var != ranum:
    >> if var == ranum:
    >> print var, 'equals to:', ranum
    >> else:
    >> print var, 'does not equal to:', ranum
    >>
    >> ########## End of Script ###########
    >>
    >>
    >> What I'm trying to do is to print the new value that ranum generates
    >> if the condition is not met. So far if you run the script it prints
    >> the same value over and over again, making in an infinite loop. What
    >> can I do in order to print out the new value generated every time
    >> the condition is not met?
    >>
    >> Thanks!
    >>
    >> Paulo Repreza

    > in your script you generate the random number only once, no wonder it
    > keep being the same value over & over.
    >
    > # Initialize ranum with a random number
    > ranum = random.randint(1,100)
    >
    > #Creating while loop. Stops until var == ranum
    > while var != ranum:
    > if var == ranum:
    > print var, 'equals to:', ranum
    > else:
    > print var, 'does not equal to:', ranum
    > ranum = random.randint(1,100) # generate a new number, that's the
    > missing line in your script
    >
    > JM


    And a more idiomatic way of writing this in Python would be, I guess:

    import random

    var = 65
    while True:
    ranum = random.randint(1, 100)
    if var == ranum:
    print var, 'equals to:', ranum
    break
    else:
    print var, 'does not equal to:', ranum

    (following up from a FU as I can't see the OP)

    --
    Arnaud
    Arnaud Delobelle, Feb 15, 2010
    #3
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,228
    Tim Slattery
    Mar 30, 2005
  2. Uday Bidkar
    Replies:
    4
    Views:
    487
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  3. globalrev
    Replies:
    4
    Views:
    756
    Gabriel Genellina
    Apr 20, 2008
  4. Chris Angelico

    random.randint() slow, esp in Python 3

    Chris Angelico, Sep 22, 2011, in forum: Python
    Replies:
    6
    Views:
    3,012
    Mark Dickinson
    Sep 24, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page