While loops


A while loop will continue to repeat a block of code while some condition is true. It checks the condition at the start of each loop and if it is False then it doesn’t run the block of code.


A while loop will always need the condition to result in True or False.

Easy example

fileName = ''
while fileName == '':
    fileName = input('Please enter a filename: ')
print('Thank you.')
Show/Hide Output
Please enter a filename:
Please enter a filename:
Please enter a filename:
Please enter a filename: test.py
Thank you.

The text Please enter a filename: will keep on displaying on the screen every time Enter is pressed. It will stop this loop if any text is entered.


while condition:
    print('do this')    #the conditions indented will repeat while the condition is true
    print('and this')

print('After, do this') #this will happen once the condition is false


Example 1 - While to check a string

userPassword = ''

while userPassword != 'secret':
    userPassword = input('Type in your password: ')

print('Access granted!')
Show/Hide Output
Type in your password: hello
Type in your password: name
Type in your password: password
Type in your password: secret
Access granted!

Example 2 - While with a flag - to continue playing a game

playGame = True

while playGame:
    print('We are playing the game')

    playAgain = input('Would you like to playAgain? ')

    if playAgain == 'n' or playAgain == 'no':
    playGame = False

print('Thank you for playing')
Show/Hide Output
We are playing the game
Would you like to playAgain? yes
We are playing the game
Would you like to playAgain? y
We are playing the game
Would you like to playAgain? no
Thank you for playing


== means is equal to. We need to use two equals symbols as = is used for assigning variables.

Example 3 - An infinite loop

while True:
Show/Hide Output

This will continue forever. Press CTRL-C to stop the program running.


Infinite loops should be avoided at all costs. Think about a different way of solving a problem to avoid them.

Example 4 - While to check a list

usernames = ['Tony', 'asmith', 'Angela']

userChoice = ''

while userChoice not in usernames:
    userChoice = input('Type in your username: ')

print('Hi ' + userChoice)
Show/Hide Output
Type in your username:
Type in your username: asm
Type in your username: asmit
Type in your username: asmith
Hi asmith

Key points


Beginners will often write while gameOver == True:. This should be rewritten while gameOver: and is easier for programmers to read.


while True: creates an infinite loop. It is possible to break out from this if a condition is met using the break keyword. In general, break is not a good technique to use as it can make code hard to debug - use flags instead.


A while loop is one way of performing iteration (looping). The other way is to use a for loop. While loops are also known as condition-controlled loops/iteration.

See also

It is important that you understand Comparison and logical operators to use loops well.