WEBVTT

00:00.170 --> 00:07.460
This is the second activity of this course, and in this activity we are going to ask the user to type

00:07.460 --> 00:08.150
something.

00:08.150 --> 00:09.290
We're going to get this.

00:09.290 --> 00:14.510
So we're going to get the input and we are going to validate the user input okay.

00:14.510 --> 00:18.080
So this is something that's done very often in programming.

00:18.110 --> 00:23.210
And before I give you the challenge I actually need to show you one more function that you didn't know.

00:23.210 --> 00:25.880
It's actually how to get the user input.

00:25.880 --> 00:33.560
So you have seen that you can print something for example with the print function like that okay.

00:33.590 --> 00:36.800
So in this case it's basically an output.

00:36.800 --> 00:41.180
So from the program to the user here the user is going to be the shell okay.

00:41.210 --> 00:43.820
It's whatever you type or that you see on the shell.

00:43.820 --> 00:46.280
So with print you have an output.

00:46.310 --> 00:54.260
Now to get the user to actually type something here and get it into your program, you can use the input

00:54.980 --> 00:56.090
function okay.

00:56.120 --> 00:58.160
So you have print and you have input.

00:58.160 --> 01:05.100
If I run this you will see the program problem is kind of hanging there, and I can just type some text

01:05.130 --> 01:12.240
or some numbers or whatever, and I press enter and you can see now the input function returns inside

01:12.240 --> 01:13.440
the parentheses.

01:13.440 --> 01:15.210
Here I can type a prompt.

01:15.240 --> 01:15.420
Okay.

01:15.450 --> 01:17.040
So that would be a bit better.

01:17.040 --> 01:23.580
So for example enter a number with a colon a space just like that.

01:23.580 --> 01:26.940
So if I run again you see now we have entered a number.

01:26.940 --> 01:28.380
So it's a bit more explicit.

01:28.380 --> 01:32.850
So now I can say for example 56 I press enter.

01:32.970 --> 01:33.480
All right.

01:33.510 --> 01:36.120
Now the thing is we don't do anything with this input.

01:36.120 --> 01:38.400
So I'm going to save this inside.

01:38.400 --> 01:46.710
So because I asked for a number I'm going to save it inside a variable named number is equal to input

01:46.740 --> 01:47.430
okay.

01:47.880 --> 01:52.920
This is first going to ask enter a number to get the input from the shell.

01:52.920 --> 01:56.550
And to store this value inside the number variable.

01:56.550 --> 01:58.140
Still it's not going to print anything.

01:58.140 --> 02:01.360
So then I can print a Uh, number.

02:01.360 --> 02:05.980
So I'm just going to print back what I typed on the terminal.

02:06.610 --> 02:07.750
Let's run this.

02:07.750 --> 02:10.120
Enter a number one, two, three.

02:10.120 --> 02:11.890
So 123.

02:11.920 --> 02:13.180
Let's press enter.

02:13.180 --> 02:15.910
And you can see 123.

02:15.910 --> 02:18.610
So we just print it back the number.

02:18.610 --> 02:21.310
But now there is one thing that you need to know.

02:21.310 --> 02:23.980
And that will be very important for you to complete the challenge.

02:23.980 --> 02:29.290
Is that what you get here, even if you ask for a number, what you get here is not a number.

02:29.290 --> 02:35.020
What you get here with the input function is a string, and that will be saved as a string.

02:35.020 --> 02:36.700
Even if you write number as a variable.

02:36.730 --> 02:40.480
Okay, the name of the variable doesn't really mean anything for the program.

02:40.570 --> 02:41.740
It's still going to be a string.

02:41.740 --> 02:49.420
How to verify that I'm going to write type here type number I'm going to print the type of the data

02:49.450 --> 02:50.710
I get okay.

02:50.740 --> 02:52.150
So for example I put one.

02:52.180 --> 02:54.310
You can see we have a string.

02:54.310 --> 02:59.110
So what we receive here inside the variable number is not one.

02:59.290 --> 03:01.860
It is the string one.

03:02.340 --> 03:02.610
Okay.

03:02.640 --> 03:10.080
So that's going to be a problem if you want to use this variable as an integer number later on.

03:10.110 --> 03:16.560
And so what we can do is we can cast a value to another type okay.

03:16.590 --> 03:24.030
So for example here to make sure that we have an integer going to number we can use the int function.

03:24.030 --> 03:26.910
And I'm going to open parentheses and close parentheses.

03:26.910 --> 03:33.420
Here you can see with the int keyword here that's with syntax highlighting I can cast whatever is in

03:33.420 --> 03:35.280
the parentheses to an integer.

03:35.280 --> 03:38.280
So what's going to happen here is we first have the input.

03:38.280 --> 03:42.090
So we're going to get the data from the shell.

03:42.090 --> 03:44.640
And we're going to cast this into an integer.

03:44.820 --> 03:50.610
Now if I run this and I put the number you can see now we have an integer number okay.

03:50.640 --> 03:52.350
So that would be helpful for our program.

03:52.350 --> 03:56.190
You can also you can cast to int but you can also cast to float.

03:56.190 --> 03:57.660
For example like this.

03:57.660 --> 04:00.930
You can cast to a string with str okay.

04:00.960 --> 04:04.090
So you can cast two different data types here.

04:04.090 --> 04:05.170
We're going to use int.

04:05.200 --> 04:05.560
Okay.

04:05.590 --> 04:08.200
And now I can explain what is going to be the challenge.

04:08.200 --> 04:09.910
So I'm going to remove this print here.

04:10.570 --> 04:12.490
And actually I'm going to change the prompt.

04:12.490 --> 04:18.400
Enter a number between 1 and 100.

04:18.400 --> 04:23.890
So we are asking the user to enter a number that should be between 1 and 100.

04:23.920 --> 04:27.490
Of course the user can type whatever they want here.

04:27.520 --> 04:34.720
And what we want to do in our program is to validate that the number is actually between 1 and 100.

04:34.720 --> 04:36.940
So you already have the first line of the program.

04:36.940 --> 04:43.840
And then you are going to need to create a condition with an if block, maybe with if and else to say

04:43.840 --> 04:46.840
that the number is wrong or the number is correct.

04:46.870 --> 04:47.380
All right.

04:47.380 --> 04:49.810
And I will see you in the next video for the solution.

04:49.810 --> 04:53.980
Make sure you take enough time to think about it to try some things.

04:54.010 --> 04:58.600
Okay, maybe go back to the previous lessons, but make sure that you practice on your own.

04:58.600 --> 05:01.750
That's really important for you to make progress.
