Use if inside while : While : Language Basics PYTHON examples


PYTHON examples » Language Basics » While »

 

Use if inside while


Use if inside while
 

import random  

# set the initial values from to 5
the_number = random.randrange(51
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop
while (guess != the_number):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."
            
    guess = int(raw_input("Take a guess: "))
    tries += 1

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!n"

           
         
  



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo Language Basics
» While