Computer programs are great to use for automating and repeating tasks so that we don’t have to. One way to repeat similar tasks is through using loops. We’ll be covering Python’s while loop in this tutorial.
A while loop implements the repeated execution of code based on a given Boolean condition. The code that is in a while block will execute as long as the while statement evaluates to True.
You can think of the while loop as a repeating conditional statement. After an if statement, the program continues to execute code, but in a while loop, the program jumps back to the start of the while statement until the condition is False.
As opposed to for loops that execute a certain number of times, while loops are conditionally based, so you don’t need to know how many times to repeat the code going in.
In Python, while loops are constructed like so:
while [a condition is True]: [do something]
The something that is being done will continue to be executed until the condition that is being assessed is no longer true.
Let’s create a small program that executes a while loop. In this program, we’ll ask for the user to input a password. While going through this loop, there are two possible outcomes:
password = ''
The empty string will be used to take in input from the user within the while loop.
Now, we’ll construct the while statement along with its condition:
password = '' while password != 'password':
Here, the while is followed by the variable password. We are looking to see if the variable password is set to the string password (based on the user input later), but you can choose whichever string you’d like.
This means that if the user inputs the string password, then the loop will stop and the program will continue to execute any code outside of the loop. However, if the string that the user inputs is not equal to the string password, the loop will continue.
Next, we’ll add the block of code that does something within the while loop:
password = '' while password != 'password': print('What is the password?') password = input()
Inside of the while loop, the program runs a print statement that prompts for the password. Then the variable password is set to the user’s input with the input() function.
The program will check to see if the variable password is assigned to the string password, and if it is, the while loop will end. Let’s give the program another line of code for when that happens:
password = '' while password != 'password': print('What is the password?') password = input() print('Yes, the password is ' + password + '. You may enter.')
The last print() statement is outside of the while loop, so when the user enters password as the password, they will see the final print statement outside of the loop.
However, if the user never enters the word password, they will never get to the last print() statement and will be stuck in an infinite loop.
An infinite loop occurs when a program keeps executing within one loop, never leaving it. To exit out of infinite loops on the command line, press CTRL + C.
When you run the program, ou’ll be prompted for a password, and then may test it with various possible inputs. Here is sample output from the program:
What is the password? hello What is the password? sammy What is the password? PASSWORD What is the password? password Yes, the password is password. You may enter.
Keep in mind that strings are case sensitive unless you also use a string method to convert the string to all lower-case (for example) before checking.
Now that we understand the general premise of a while loop, let’s create a command-line guessing game that uses a while loop effectively. To best understand how this program works, you should also read about using conditional statements and converting data types.
First, we’ll create a program. We want the computer to come up with random numbers for the user to guess, so we’ll import the random module with an import statement.
Next, we’ll assign a random integer to the variable number, and keep it in the range of 1 through 25 (inclusive), in the hope that it does not make the game too difficult.
import random number = random.randint(1, 25)
At this point, we can get into our while loop, first initializing a variable and then creating the loop.
import random number = random.randint(1, 25) number_of_guesses = 0 while number_of_guesses < 5: print('Guess a number between 1 and 25:') guess = input() guess = int(guess) number_of_guesses = number_of_guesses + 1 if guess == number: break
We’ve initialised the variable number_of_guesses at 0, so that we increase it with each iteration of our loop so that we don’t have an infinite loop. Then we added the while statement so that the number_of_guesses is limited to 5 total. After the fifth guess, the user will return to the command line, and for now, if the user enters something other than an integer, they’ll receive an error.
Within the loop, we added a print() statement to prompt the user to enter a number, which we took in with the input() function and set to the guess variable. Then, we converted guess from a string to an integer.
Before the loop is over, we also want to increase the number_of_guesses variable by 1 so that we can iterate through the loop 5 times.
Finally, we write a conditional if statement to see if the guess that the user made is equivalent to the number that the computer generated, and if so we use a break statement to come out of the loop.
Though it works, right now the user never knows if their guess is correct and they can guess the full 5 times without ever knowing if they got it right. Sample output of the current program looks like this:
Guess a number between 1 and 25: 11 Guess a number between 1 and 25: 19 Guess a number between 1 and 25: 22 Guess a number between 1 and 25: 3 Guess a number between 1 and 25: 8
Let’s add some conditional statements outside of the loop so that the user is given feedback as to whether they correctly guess the number or not. These will go at the end of our current file.
import random number = random.randint(1, 25) number_of_guesses = 0 while number_of_guesses < 5: print('Guess a number between 1 and 25:') guess = input() guess = int(guess) number_of_guesses = number_of_guesses + 1 if guess == number: break if guess == number: print('You guessed the number in ' + str(number_of_guesses) + ' tries!') else: print('You did not guess the number. The number was ' + str(number))
At this point, the program will tell the user if they got the number right or wrong, which may not happen until the end of the loop when the user is out of guesses.
To give the user a little help along the way, let’s add a few more conditional statements into the while loop. These can tell the user whether their number was too low or too high, so that they can be more likely to guess the correct number. We’ll add these before our if guess == number line.
import random number = random.randint(1, 25) number_of_guesses = 0 while number_of_guesses < 5: print('Guess a number between 1 and 25:') guess = input() guess = int(guess) number_of_guesses = number_of_guesses + 1 if guess < number: print('Your guess is too low') if guess > number: print('Your guess is too high') if guess == number: break if guess == number: print('You guessed the number in ' + str(number_of_guesses) + ' tries!') else: print('You did not guess the number. The number was ' + str(number))
When we run the program again , we see that the user gets more guided assistance in their guessing. So, if the randomly-generated number is 12 and the user guesses 18, they will be told that their guess is too high, and they can adjust their next guess accordingly.
There is more that can be done to improve the code, including error handling for when the user does not input an integer, but in this example we see a while loop at work in a short command-line program.
Set a variable called total to equal 0. Then while total is not equal (!=) to 4 ask the user to input a guess for a number between 1 and 10.
Write a program that has a stored password. Ask the user to guess the password and display a success message shown they get it right.
Write a program that asks the user to enter a prime number and only give a success message when they have typed 3 consecutively.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.