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

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

Jean-Michel Pichavant
Guest
Posts: n/a

 02-15-2010
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

Bruno Desthuilliers
Guest
Posts: n/a

 02-15-2010
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
>>

Arnaud Delobelle
Guest
Posts: n/a

 02-15-2010
Jean-Michel Pichavant <(E-Mail Removed)> 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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Isaac Won Python 9 03-04-2013 10:08 AM Roedy Green Java 3 09-13-2008 01:51 AM Daniel Pitts Java 14 12-23-2006 05:22 AM Uday Bidkar C++ 4 12-12-2006 12:30 PM Steven Java 5 03-30-2005 09:19 PM

Advertisments