WEBVTT

00:00.050 --> 00:05.930
This is the solution for the activity number two, where you have to validate the user input.

00:05.930 --> 00:07.910
And I already gave to you.

00:07.940 --> 00:09.440
Well the function input.

00:09.440 --> 00:11.270
So you can get the user input.

00:11.270 --> 00:14.330
And also how to cast it into an integer.

00:14.360 --> 00:16.310
Just one thing I'm going to run this.

00:16.310 --> 00:19.850
And what I can provide a number any number is going to work.

00:19.850 --> 00:22.910
But let's say I provide a string.

00:22.940 --> 00:28.760
You will see that we get an error because we cannot cast a string to an integer.

00:28.760 --> 00:34.310
So already with this program, we are sure that whatever is after line one is going to be an integer

00:34.310 --> 00:34.880
number.

00:34.880 --> 00:43.340
And now I'm just going to save this as activity two dot p y.

00:44.240 --> 00:45.980
So let's save the file.

00:46.340 --> 00:46.610
Okay.

00:46.640 --> 00:49.220
So you will be able to download this file as well.

00:49.220 --> 00:51.980
And let's write our condition here.

00:51.980 --> 00:53.090
So we have a number.

00:53.120 --> 00:56.420
How do we validate that it's between 1 and 100.

00:56.420 --> 00:58.130
So there are actually two ways to do that.

00:58.130 --> 01:01.540
You can either check if it's between 1 and 100.

01:01.570 --> 01:05.740
Or you can check if it's actually lower than zero or greater than 100.

01:05.770 --> 01:05.950
Okay.

01:05.980 --> 01:12.310
When you want to validate a user input, you can check if it's correct or you can check if it's wrong.

01:12.310 --> 01:15.310
And usually we're going to check if it's wrong first.

01:15.310 --> 01:17.110
So there are many examples of that.

01:17.110 --> 01:23.200
For example, if you if you go online, you create a new account in a website and you need to set up

01:23.200 --> 01:27.880
a password that's at least, for example, 10 or 12 characters.

01:27.880 --> 01:33.940
Usually what you will see is that if the password is less than 12 characters, you will get a message

01:33.940 --> 01:37.330
saying the password needs to be at least 12 characters.

01:37.330 --> 01:38.140
So you get an error.

01:38.170 --> 01:42.820
You get a message to tell you that the length of the password is wrong, but they don't really tell

01:42.820 --> 01:43.360
you anything.

01:43.390 --> 01:46.150
If the password is 14 or 15 or 20 characters.

01:46.180 --> 01:51.340
Okay, the important thing is just to make sure that the length of the password is not wrong.

01:51.370 --> 01:57.460
Okay, so you can see very often to validate that the user input is correct, we actually verify that

01:57.460 --> 01:59.590
it is not wrong okay.

01:59.620 --> 02:01.110
And that's what we're going to do here.

02:01.110 --> 02:02.520
I'm going to do if.

02:02.760 --> 02:06.090
So we want the number to be minimum one.

02:06.090 --> 02:15.810
So I can say that if the number is lower or equal than zero, or if the number is greater strictly than

02:15.810 --> 02:20.340
100, then the number is wrong.

02:20.370 --> 02:20.700
Okay.

02:20.730 --> 02:27.780
So once again if the number you provide is zero or less, we are already out of the valid range.

02:27.780 --> 02:34.290
Then if the number is greater, so strictly greater than 100 because 100 is valid, then we are also

02:34.290 --> 02:37.050
out of the range of accepted values.

02:37.260 --> 02:37.680
Okay.

02:37.680 --> 02:43.770
So with the combination of those two conditions here with the Or keyword, we check if this is true.

02:44.070 --> 02:49.020
So for example if number is minus one then well this is going to be true.

02:49.020 --> 02:52.020
And then if the number is well greater than zero.

02:52.020 --> 02:55.980
But if it's also greater than 100 this is going to be true.

02:56.100 --> 03:00.240
So we can print wrong number.

03:01.430 --> 03:04.280
So wrong number.

03:05.600 --> 03:07.310
And actually let's print.

03:07.520 --> 03:08.600
Let's print the number.

03:08.600 --> 03:09.110
So.

03:09.140 --> 03:13.430
To concatenate two strings you can do plus okay.

03:13.460 --> 03:16.160
And number this is going to give us an error.

03:16.190 --> 03:17.750
I'm just going to come back in a minute.

03:17.750 --> 03:22.130
And just to note here is I have used the lower or equal than zero.

03:22.160 --> 03:25.430
I could have used strictly lower than one.

03:25.460 --> 03:25.820
Okay.

03:25.850 --> 03:29.450
You would have also worked here for integers.

03:29.480 --> 03:31.610
I'm just going to keep this one.

03:31.700 --> 03:33.860
And so let's actually try this.

03:33.860 --> 03:36.530
Let's try this with a wrong number.

03:36.800 --> 03:39.710
So enter a number between 1 and 100.

03:39.710 --> 03:42.260
Let's say I put 200.

03:42.710 --> 03:44.180
Let's see what we have.

03:44.210 --> 03:54.470
Well we have an error because you can see on line four you can only concatenate string with string not

03:54.470 --> 03:55.340
integers.

03:55.340 --> 03:56.510
Why we have this error.

03:56.510 --> 03:58.430
Because now number is an integer.

03:58.430 --> 04:00.140
We just cast it to an integer.

04:00.170 --> 04:03.320
Now it says that you can only concatenate string with string.

04:03.320 --> 04:04.910
So this is a string.

04:05.270 --> 04:06.860
This is an integer.

04:06.860 --> 04:08.870
So that's that's a problem.

04:08.870 --> 04:10.070
How do we fix this.

04:10.070 --> 04:13.400
Well we can cast this integer to a string.

04:13.400 --> 04:16.400
So we do str number.

04:16.400 --> 04:22.220
So basically we cast back the number to a string and make sure you add the correct amount of parentheses.

04:22.250 --> 04:22.940
All right.

04:22.970 --> 04:26.960
So we have a string plus a string.

04:26.990 --> 04:29.270
Let's run again.

04:29.270 --> 04:31.310
And let's put 200 again.

04:31.400 --> 04:34.370
You can see wrong number 200.

04:34.640 --> 04:36.710
Now if I put a correct number.

04:36.710 --> 04:39.830
So let's say one one should work.

04:39.860 --> 04:42.050
You can see we don't print anything.

04:42.050 --> 04:44.300
So maybe we could say that it's correct.

04:44.300 --> 04:45.800
So how do we do this.

04:45.830 --> 04:51.320
We just create an else block print correct number.

04:55.280 --> 04:57.920
You could also print the number again if you want.

04:57.950 --> 04:59.900
Now let's run again.

04:59.900 --> 05:03.520
So enter a number between 1 and 101.

05:03.550 --> 05:05.170
You can see the correct number.

05:05.170 --> 05:06.850
We have validated the number.

05:07.000 --> 05:09.610
Let's run again with.

05:09.700 --> 05:13.180
Let's test the edge cases.

05:13.180 --> 05:16.420
So 100 100 is the correct number.

05:16.450 --> 05:17.110
Okay.

05:17.380 --> 05:22.810
Now for example if I put zero or if I put minus one you see wrong number.

05:22.840 --> 05:23.260
Great.

05:23.260 --> 05:26.470
And we have successfully validated the user input.

05:26.470 --> 05:28.060
And as an additional challenge.

05:28.060 --> 05:29.710
So I'm not going to give you the solution here.

05:29.710 --> 05:31.750
But you could do the opposite.

05:31.750 --> 05:36.370
So instead of checking if it's wrong you will check if it's true.

05:36.370 --> 05:43.060
And for this you will also need to combine two conditions with the statement and keyword.

05:43.090 --> 05:43.390
Okay.

05:43.420 --> 05:49.210
And you can easily test actually if your code is working, because when you put a number between 1 and

05:49.210 --> 05:51.760
100, you should see that it's a correct number.

05:51.790 --> 05:52.120
All right.

05:52.120 --> 05:56.860
But that's if you want to do additional practice with conditions then you can continue with loops.

05:56.860 --> 06:01.540
And we are getting close to the end for the Python basics for this course.
