WEBVTT

00:00.080 --> 00:06.290
This is the solution for the activity for where you have to get the user input, and depending on that

00:06.290 --> 00:10.760
user input, you're going to turn off or turn on the LED.

00:10.760 --> 00:13.490
And we're going to make sure that the user gives us 0 or 1.

00:13.490 --> 00:17.120
If it's any other value, we're going to print an error message.

00:17.120 --> 00:21.230
So let's get started I'm first going to remove this because this is not code.

00:21.260 --> 00:24.230
It was just some instructions that I gave you.

00:24.230 --> 00:26.390
And well I already have this.

00:26.390 --> 00:29.300
So I have a variable called user choice.

00:29.300 --> 00:30.650
And I get the input.

00:30.650 --> 00:35.150
I cast this as an integer and I can have a prompt.

00:35.150 --> 00:36.860
So let's not leave this empty.

00:36.890 --> 00:39.800
Let's try to prompt that makes sense okay.

00:39.830 --> 00:42.410
And that gives the user enough information.

00:42.410 --> 00:43.970
So we get the correct value.

00:43.970 --> 00:59.870
So I can say for example enter zero to turn off the LED and then one to turn it on with a column and

00:59.870 --> 01:00.620
a space.

01:00.620 --> 01:02.480
So we have our user input.

01:02.510 --> 01:07.190
Now, before we go any further, let's actually initialize our LED.

01:07.370 --> 01:10.280
So I'm going to put that a bit lower.

01:10.280 --> 01:12.380
And I'm going to start with the inputs.

01:12.380 --> 01:17.150
So as best practice just put all the inputs at the beginning of your program.

01:17.150 --> 01:20.120
So we will need from Gpio zero.

01:20.150 --> 01:23.330
We will need the so import LED.

01:23.420 --> 01:30.620
And then because we also want to wait some time in the program, I'm just going to do import time so

01:30.620 --> 01:32.210
that we can do Time.sleep.

01:32.240 --> 01:32.630
Great.

01:32.630 --> 01:34.280
So that's all for the import.

01:34.310 --> 01:36.230
Now we can initialize the LED.

01:36.320 --> 01:39.200
So once again let's keep things simple.

01:39.200 --> 01:43.310
Let's create an LED variable is equal to LED uppercase.

01:43.310 --> 01:48.860
And then in the parentheses we specify the Gpio which is still 17.

01:48.890 --> 01:50.330
Okay we haven't changed the circuit.

01:50.330 --> 01:52.040
So now the LED is initialized.

01:52.040 --> 01:56.240
And we're going to ask the user input.

01:56.270 --> 01:57.770
So I just put this here.

01:57.800 --> 01:58.190
Okay.

01:58.220 --> 02:00.410
You can see I just add some spaces.

02:00.440 --> 02:02.900
It's just for the readability of the program.

02:02.930 --> 02:03.140
Okay.

02:03.170 --> 02:06.770
You could add zero spaces, but then it's going to be a bit hard to read.

02:06.800 --> 02:07.190
Great.

02:07.190 --> 02:09.740
And what do I do with this user choice.

02:09.770 --> 02:11.870
Well here I'm going to use an if.

02:11.960 --> 02:17.780
And I'm going to do if user choice is equal to zero.

02:17.780 --> 02:20.960
And so pay attention here is not just one equal sign.

02:20.960 --> 02:23.060
It's two equal sign.

02:23.090 --> 02:23.330
Okay.

02:23.360 --> 02:30.440
So once again one equal sign is used to assign a value from the right to the left.

02:30.440 --> 02:33.710
But two equal sign is going to compare two values.

02:33.740 --> 02:37.730
So for example with zero and then a colon.

02:37.730 --> 02:43.400
So it's going to compare those two values and return true or false.

02:43.430 --> 02:43.610
Okay.

02:43.640 --> 02:47.450
So true if they are equal and false if they are not equal.

02:47.450 --> 02:52.490
So if the user choice is equal with two equal to zero what do we want to do.

02:52.520 --> 03:00.620
Well we want to turn off the LED so we can do LED, sort of as simple as that.

03:00.620 --> 03:04.250
Then I go back to a new line and that's it for this block of code.

03:04.250 --> 03:05.600
We don't want to do anything else.

03:05.600 --> 03:11.630
So I'm going to go back to the previous indentation and add an L if okay.

03:11.660 --> 03:14.390
So if the user choice this time is equal.

03:14.390 --> 03:18.800
So user choice is equal with double equal to one.

03:18.830 --> 03:20.810
Then I put a colon.

03:20.810 --> 03:25.340
We go back to a new line with an indentation LED dot on.

03:26.000 --> 03:27.710
So I turn on the LED.

03:27.740 --> 03:29.660
So you might think well that's it.

03:29.660 --> 03:31.910
But we also want to validate the input.

03:31.910 --> 03:38.720
Which means that if the value is not correct we want to also say, well the choice is an invalid choice.

03:38.720 --> 03:41.060
It must be 0 or 1.

03:41.060 --> 03:45.080
So I'm going to go back to a new line again previous indentation.

03:45.080 --> 03:47.000
And I can just put else.

03:47.000 --> 03:54.500
So whatever value that is not 0 or 1 is going to fall under the else else I go back to a new line with

03:54.500 --> 03:57.770
an indentation and I can put for example, print.

03:58.430 --> 04:04.610
Invalid choice must be 0 or 1.

04:04.640 --> 04:08.960
You can also decide to print the choice if you want.

04:08.990 --> 04:15.050
In this case, make sure that you cast this as a string, because here it's an integer.

04:15.050 --> 04:16.250
So we print that.

04:16.250 --> 04:19.130
And then I'm also going to add exit here.

04:19.130 --> 04:22.070
And I will tell you why in just one minute.

04:22.070 --> 04:27.950
Because what we want to do next is that after we turn off or we turn on the LED, we just want to wait

04:27.950 --> 04:29.450
for two seconds.

04:29.450 --> 04:38.120
So what I could do is to do time dot sleep with two and then time to sleep with two here, etc. but

04:38.150 --> 04:45.920
I'm just going to put the time.sleep after this if like this time.sleep with two.

04:46.250 --> 04:52.310
Which means that if we turn off or if we turn on, the LED is going to wait for two seconds anyway,

04:52.310 --> 04:55.430
so we just need to write Time.sleep only once.

04:55.430 --> 05:03.050
And in this case I have added the exit keyword here because if the choice is invalid, we don't want

05:03.050 --> 05:04.280
to wait for two seconds.

05:04.280 --> 05:07.850
So in this case we just exit from this point.

05:07.880 --> 05:11.780
Okay, so to recap we first do all the imports that we need.

05:11.810 --> 05:13.250
We initialize the LED.

05:13.250 --> 05:16.400
We get the user input that we cast as an int.

05:16.400 --> 05:20.060
And then we check if it's zero we power it off.

05:20.060 --> 05:21.170
So we power off the LED.

05:21.290 --> 05:23.840
If it's one we power on the LED.

05:23.930 --> 05:29.090
And also so after this, after either this instruction or that instruction, we're going to go out of

05:29.090 --> 05:31.490
the if and wait for two seconds.

05:31.490 --> 05:36.470
But still in the if if the user choice is not 0 or 1, we print an error message.

05:36.470 --> 05:40.010
So error message is basically just a normal message.

05:40.010 --> 05:42.920
And then we also exit from the program.

05:42.920 --> 05:45.470
So we don't execute that line of code.

05:45.470 --> 05:49.490
And maybe I can just print end here.

05:49.490 --> 05:53.090
So it's going to be even easier to see when the program ends.

05:53.120 --> 05:53.570
All right.

05:53.570 --> 05:55.790
So let's start this.

05:55.790 --> 06:01.610
And as you can see when you initialize the LED it's going to be turned off by default.

06:01.610 --> 06:06.590
And we have enter 0 or 1 here I'm just going to go with one.

06:06.590 --> 06:08.480
For example I press enter.

06:08.480 --> 06:13.070
You see the LED is turned on and after two seconds we have end.

06:13.190 --> 06:14.360
We can start again.

06:15.170 --> 06:17.450
And then I will say zero.

06:17.780 --> 06:20.810
So actually the LED is already powered off.

06:20.990 --> 06:24.920
But if I do zero well you see the LED is still powered off.

06:24.920 --> 06:26.240
And then we have end.

06:26.240 --> 06:29.360
And let's test the last case.

06:29.390 --> 06:31.310
Let's say I give a wrong value.

06:31.310 --> 06:36.830
So three in this case you see invalid choice must be 0 or 1.

06:36.830 --> 06:39.590
And actually here you see we still have the end.

06:39.620 --> 06:43.130
We still have the time.sleep because it's my mistake.

06:43.130 --> 06:44.450
I gave you the exit keyword.

06:44.450 --> 06:46.490
It's actually the exit function.

06:46.520 --> 06:46.700
Okay.

06:46.700 --> 06:48.800
You can see it can be very different.

06:48.830 --> 06:53.930
He exit is a real keyword, but it's not going to do the same thing as the function.

06:53.960 --> 06:56.600
So the function is going to actually exit from the program.

06:56.600 --> 06:59.570
And to call the function you have to use parentheses.

06:59.600 --> 06:59.750
Okay.

06:59.750 --> 07:00.830
Let's try again.

07:01.250 --> 07:02.960
I'm going to give three again.

07:04.340 --> 07:04.640
Okay.

07:04.670 --> 07:06.380
And you can see we have.

07:06.410 --> 07:09.080
So we even have another message here.

07:09.080 --> 07:12.080
Process ended with exit code zero.

07:12.080 --> 07:16.400
And this time we didn't wait for two seconds and we don't print the end.

07:16.430 --> 07:16.760
All right.

07:16.790 --> 07:22.040
And I'm going to leave this mistake here in the video just to show you that as you can see, sometimes

07:22.070 --> 07:23.600
things will work.

07:23.600 --> 07:28.670
So you're not going to get an error when you run the code, but it's not going to work as you want because

07:28.670 --> 07:33.710
maybe you forgot some parentheses, or maybe you added extra parentheses that you shouldn't have.

07:33.740 --> 07:39.230
Or you can see there can be a lot of different things like that that will give you a behavior that you

07:39.230 --> 07:39.980
don't want.

07:40.010 --> 07:42.560
So pay attention to everything you type, okay.

07:42.560 --> 07:45.200
And double check your code before you run it.

07:45.230 --> 07:45.710
All right.

07:45.710 --> 07:47.540
And that's the end of this activity.
