WEBVTT

00:00.000 --> 00:10.000
In this video we will understand while loop.

00:10.000 --> 00:16.000
Comparison between 4 and while loops.

00:16.000 --> 00:22.000
Let's have a quick look at the secret number guessing game we previously developed using a 4 loop.

00:22.000 --> 00:28.000
We used the random module to pick a secret number between 1 and 20.

00:28.000 --> 00:32.000
The player had 5 attempts to guess it.

00:32.000 --> 00:38.000
Each guess was checked using if, else if, and else conditional statements.

00:38.000 --> 00:43.000
If the guess matched the secret number, we printed congratulations.

00:43.000 --> 00:46.000
You have guessed the secret number.

00:46.000 --> 00:50.000
Else if the guess was too high, we printed too high.

00:50.000 --> 00:56.000
Else the guess was too low, we printed too low.

00:56.000 --> 01:01.000
A 4 loop limited attempts to 5, with a break statement for a correct guess.

01:01.000 --> 01:05.000
If the player failed, we used an F string to display the correct number.

01:05.000 --> 01:11.000
Print, F, sorry, the correct number was secret number.

01:11.000 --> 01:17.000
Now, let's learn how to build the same program using a while loop for repeated attempts.

01:17.000 --> 01:21.000
First import random, set a random number between 1 and 100.

01:21.000 --> 01:24.000
Secret number equal to random dot random.

01:24.000 --> 01:27.000
Open parenthesis, 1 comma 100.

01:27.000 --> 01:30.000
Close parenthesis.

01:30.000 --> 01:35.000
Next, we keep track of how many guesses you make using a counter called attempts.

01:35.000 --> 01:37.000
Set the initial attempt count.

01:37.000 --> 01:39.000
Attempts equals 0.

01:39.000 --> 01:41.000
Now write instructions for player.

01:41.000 --> 01:46.000
Print, welcome to the guessing game.

01:46.000 --> 01:50.000
Print, I have selected a number between 1 and 100.

01:50.000 --> 01:56.000
Try to guess it.

01:56.000 --> 02:01.000
Now, here is the main difference between 4 loop and while loop.

02:01.000 --> 02:04.000
A 4 loop repeats a set.

02:04.000 --> 02:07.000
Number of times usually when you know how many times you need to loop.

02:07.000 --> 02:11.000
But a while loop keeps repeating as long as a condition is true.

02:11.000 --> 02:19.000
Here, we will see how we can use a while loop to keep the game going until the player guesses the correct number.

02:19.000 --> 02:22.000
While true.

02:22.000 --> 02:26.000
To get users guess, write guess equal to int input.

02:26.000 --> 02:29.000
Enter your guess.

02:29.000 --> 02:31.000
Attempts plus equals to 1.

02:31.000 --> 02:37.000
It gives us another chance to try again until you guess the right number.

02:37.000 --> 02:39.000
Check the player's guess.

02:39.000 --> 02:44.000
Add conditions to check if the guess is correct, too high or too low.

02:44.000 --> 02:47.000
We will add an if condition.

02:47.000 --> 02:50.000
If guess is less than the secret number.

02:50.000 --> 02:53.000
Print, too low.

02:53.000 --> 02:54.000
Try again.

02:54.000 --> 02:57.000
Now add an else if and else conditions.

02:57.000 --> 03:00.000
LF guess is greater than the secret number.

03:00.000 --> 03:02.000
Print, too high.

03:02.000 --> 03:03.000
Try again.

03:03.000 --> 03:06.000
Else, print, F.

03:06.000 --> 03:07.000
Congratulations.

03:07.000 --> 03:10.000
You have guessed the correct number.

03:10.000 --> 03:13.000
Secret number in attempts.

03:13.000 --> 03:14.000
Attempts.

03:14.000 --> 03:18.000
When you guess the right number, the computer congratulates you.

03:18.000 --> 03:21.000
It even tells you how many attempts it took you.

03:21.000 --> 03:26.000
Once you guess correctly, the while loop is told to stop by the word break.

03:26.000 --> 03:28.000
So we add break statement here.

03:28.000 --> 03:33.000
Save the program with a name like number guessing game.py.

03:33.000 --> 03:38.000
Click the run button and check how many attempts it takes to find the secret number.

03:45.000 --> 03:50.000
Look, we guessed the correct number in 9 attempts.

03:50.000 --> 03:53.000
Now you know how to use a while loop to keep the game running.

03:53.000 --> 03:56.000
Keep practicing and happy coding.

