Till now we have seen python for loop. But when you need a loop to exit at certain condition. We can use Python While Loop.E.g exit the loop when user number of cats exceeds number of dogs.

Wait, you have been keeping up with the terminology, right? That if we write a line and end it with a :(colon) then that tells Python to start a new block of code? Then we indent and that’s the new code. This is all about structuring your programs so that Python knows what you mean. If you do not get that idea then go back and do some more work with if-statements, functions, and the for-loop until you get it.

Later on we’ll have some exercises that will train your brain to read these structures, similar to how we burned boolean expressions into your brain.

Back to python while loop. What they do is simply do a test like an if-statement, but instead of running the code block once, they jump back to the “top” where the while is, and repeat. A while-loop runs until the expression is False.

Here’s the problem with while-loops: Sometimes they do not stop. This is great if your intention is to just keep looping until the end of the universe. Otherwise you almost always want your loops to end eventually.

To avoid these problems, there are some rules to follow:

  1. Make sure that you use while-loops sparingly. Usually a for-loop is better.
  2. Review your while statements and make sure that the boolean test will become False at some point.
  3. When in doubt, print out your test variable at the top and bottom of the while-loop to see what it’s doing.


i = 0
numbers = []
while i < 3:
    print ("At the top i is %d" % (i))
    i = i + 1
    print ("Numbers now: ", numbers)
    print ("At the bottom i is %d" % (i))
print ("The numbers: ")
for num in numbers:
    print (num)

Save and run this script. As long as i < 3 python while loop will execute and your script will work. Once i = 3 it will break.


python while loop

I think its easy to understand output of while loop. Now for this chapter I have a Game made with help of python while loop. I am not going to explain how this game work. You just save the script and run it. Try to understand what this script do.


I am importing a module called Random. This module is used to select a number randomly from a range.

import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
print('Well, ' + myName + ', I am thinking of a number between 1 and 50.')
number = random.randint(1, 50) # To Guess any number between 1-50
guess = 0 # Setting initial value of guess as 0
while guess != number:
    print('Take a guess.') # There are four spaces in front of print.
    guess = input()
    guess = int(guess)
    guessesTaken = guessesTaken + 1
    if guess < number:
        print('Your guess is too low.') # There are eight spaces in front of print.
    if guess > number:
        print('Your guess is too high.')
    if guess == number:
        print ("Perfect you finally found the number, it took you %d attemps" % (guessesTaken))


no comments

Leave me comment