Python while loop

Introduction to python while loop:

In computer programming, we may come across situations wherein we may require to execute same code repeatedly. Looping simply means execution of specific code again and again till the provided condition gets satisfied. There are many different ways of looping available in python. So in previous article we have covered python for loop and in this article we’ll be covering python while loop in detail.

What is python while loop:

Sometimes we can’t predict about when the test condition will be satisfied. Therefore we can’t even predict about how many times the code(While block) will be iterated or repeatedly executed. So while loop is basically used in situations wherein we don’t know the number of code repetitions/iterations before the actual code execution. While loop in python will keep on repeating the code execution as long as the test condition is evaluated to True.

Code block written in while loop is also known as While block. This while block is separated from other code using indentation. Click here to learn more about, what is indentation in python?

Python while loop is implemented in such a way that, Every time after the while block execution it jumps back to the starting point of while statement and checks the test condition. The time when test condition evaluates to False, while loop is exited.

Major difference between For Loop and While Loop is that, 

  • For Loop: Number of iteration in for loop are fixed i.e number of repetitions are know upfront before actual code execution.
  • While Loop: Number of code repetitions are conditional so we don’t know the number of times the code is going to be repeated.

Python while loop flowchart:

while loop in python | python while loop

Syntax of while loop in python:

while [Test condition is True]
     [Execute the code]
  • while loop execution steps:
    • Firstly it checks the Test Condition.
    • If the test evaluates to True then it executes the code while block.
    • After one iteration of while block execution it again goes back to the initial position and checks the test condition again.
    • This process continues till the time Test condition evaluates to False.

Example of while loop in python:

Let’s try to create one computer game for the practice purpose. Here we are asking the user to guess the random number between 0-10 for three times. User can input the three guess numbers if his guess is correct then he wins the game.

  • For this example firstly we’ll need some code which will generate one random number between 0-10.

To do that, we’ll be importing standard module named “random”. In this module randint() function is defined which we are going to use for random number generation.

import random
Guess_Count = 0
maximum_guess_count = 3
Generated_Number = random.randint(1, 10)
while Guess_Count < maximum_guess_count:
    Guess_Number = input("Enter the number:")
    maximum_guess_count = maximum_guess_count - 1
    if int(Guess_Number) == Generated_Number:
        print("You guessed it right!!! Random Number is:", Generated_Number)
    if maximum_guess_count == 0:
        print("Sorry, You Lost the Game! Random Number was", Generated_Number)
    print("Try one more time")

In above example we have used following logic:

  • First we have initiated variables ‘Guess_Count’ and ‘maximum_guess_count’.
  • Created new variable named ‘Generated_Number’ to store the random generated number.
  • Started the while loop which will repeat three times.
  • The time when the User guess number matched with generated number while loop will exit.
  • Also if the user guess the incorrect number for three times then also the while loop will exit.

Continue Reading:

Leave a Comment