WEBVTT

00:00.000 --> 00:12.920
In this video, we will recall Python basics, combining loops and conditionals to create

00:12.920 --> 00:20.400
an interactive game.

00:20.400 --> 00:23.760
Python is a simple yet powerful programming language.

00:23.760 --> 00:28.640
Python syntax is like writing instructions in a clear, simple language.

00:28.640 --> 00:32.640
Do you remember the secret number guessing game we developed where hints helped you find

00:32.640 --> 00:33.640
the right number?

00:33.640 --> 00:35.640
Let's recall that.

00:35.640 --> 00:40.640
Here, we import a library random, which allows the program to pick a secret number between

00:40.640 --> 00:43.480
1 and 20 using the random module.

00:43.480 --> 00:48.840
We use variables to store data, like numbers, words, or lists.

00:48.840 --> 00:53.000
It can be reused and changed, making your programs more flexible.

00:53.000 --> 00:56.440
In Python, four main data types are used.

00:56.440 --> 01:02.000
String stores text inside quotation marks, integer stores the whole numbers, float stores

01:02.000 --> 01:09.160
numbers with decimal points, and Boolean holds values of either true or false.

01:09.160 --> 01:16.160
Now here, secret number is a variable that stores a random number, generated by the function,

01:16.160 --> 01:23.240
random dot rand int, 1 comma 20, which gives a random value between 1 and 20.

01:23.240 --> 01:27.880
It sets a new secret number each time you start the game.

01:27.880 --> 01:31.160
We have given the player five attempts.

01:31.160 --> 01:34.240
Can you guess the variable and the value in this?

01:34.240 --> 01:35.240
Right.

01:35.240 --> 01:39.440
Attempt is a variable and five is the value.

01:39.440 --> 01:46.160
We store the number of attempts in a variable and used input function to let the player make

01:46.160 --> 01:47.160
a guess.

01:47.160 --> 01:52.480
Then, we used print function to give hints and instructions.

01:52.480 --> 01:56.000
After each guess, the program gave feedback.

01:56.000 --> 02:02.360
We used conditional statements, if, else if, and else to check the player's guess.

02:02.360 --> 02:06.520
If the guess matched the secret number, we printed, congratulations.

02:06.520 --> 02:08.320
You have guessed the secret number.

02:08.320 --> 02:14.280
Else if the guess was too high, we printed too high.

02:14.280 --> 02:18.800
Else the guess was too low, we printed too low.

02:18.800 --> 02:22.520
We used a for loop to give the player exactly five chances.

02:22.520 --> 02:26.840
If the guess was right, we ended the game early with a break statement.

02:26.840 --> 02:31.280
And finally, we used in formatted string to display the correct number, if the player

02:31.280 --> 02:33.280
ran out of attempts.

02:33.280 --> 02:38.560
Now, it's time to take these skills to the next level.

02:38.560 --> 02:43.280
Let's learn how can we build this same program using while loop.

02:43.840 --> 02:49.160
First, we will remove this part of for loop and let's continue the program.

02:49.160 --> 02:50.320
Create the loop.

02:50.320 --> 02:52.960
Write the main game logic using a while loop.

02:52.960 --> 03:00.240
While attempts greater than zero, guess equal to int input.

03:00.240 --> 03:03.000
Guess the number between one and twenty.

03:03.000 --> 03:08.480
This keeps the game running until the player guesses the number or runs out of attempts.

03:08.480 --> 03:12.840
The int input function ensures that the guess is treated as a number.

03:12.880 --> 03:14.440
Click the player's guess.

03:14.440 --> 03:20.400
Now, add conditions to check if the guess is correct, too high or too low.

03:20.400 --> 03:28.320
We will add a if condition, if guess equal to secret number.

03:28.320 --> 03:32.120
Print, congratulations.

03:32.120 --> 03:36.160
You have guessed the correct number.

03:36.160 --> 03:38.480
Then add a break statement.

03:38.480 --> 03:41.400
The break statement stops the loop at that moment.

03:41.400 --> 03:47.560
When the player guesses the secret number correctly, there's no need to continue asking for guesses.

03:47.560 --> 03:56.880
We will add an else, if, and else, condition, else, if, guess greater than secret number.

03:56.880 --> 04:03.080
Print, too high, else, print, too low.

04:03.080 --> 04:07.720
After each guess, reduce the number of attempts and show how many are left.

04:07.720 --> 04:11.040
Attempts minus is equal to one.

04:11.880 --> 04:14.000
F, you have attempts.

04:14.000 --> 04:17.200
Attempts left dot backslash n.

04:17.200 --> 04:21.040
If the player uses all their chances, let them know the correct number.

04:21.040 --> 04:23.520
If attempts is equal to zero.

04:23.520 --> 04:29.840
Print, F, sorry, you have run out of attempts.

04:29.840 --> 04:34.520
The correct number was secret number.

04:34.520 --> 04:39.280
It is important to add formatted string, so we get the secret number printed in the console

04:39.280 --> 04:41.760
after all incorrect attempts.

04:41.760 --> 04:48.360
Save the program with the name like numberguessinggame.pi.

04:48.360 --> 04:56.840
Click the Run button and check whether we can find the secret number.

04:56.840 --> 04:58.400
Congratulations.

04:58.400 --> 05:04.920
You have just built a number guessing game using a while loop in Python.

05:04.920 --> 05:08.680
Keep practicing, and soon you'll be creating even more exciting programs.

