This is the solution for the activity for where you have to get the user input, and depending on that
user input, you're going to turn off or turn on the LED.
And we're going to make sure that the user gives us 0 or 1.
If it's any other value, we're going to print an error message.
So let's get started I'm first going to remove this because this is not code.
It was just some instructions that I gave you.
And well I already have this.
So I have a variable called user choice.
And I get the input.
I cast this as an integer and I can have a prompt.
So let's not leave this empty.
Let's try to prompt that makes sense okay.
And that gives the user enough information.
So we get the correct value.
So I can say for example enter zero to turn off the LED and then one to turn it on with a column and
a space.
So we have our user input.
Now, before we go any further, let's actually initialize our LED.
So I'm going to put that a bit lower.
And I'm going to start with the inputs.
So as best practice just put all the inputs at the beginning of your program.
So we will need from Gpio zero.
We will need the so import LED.
And then because we also want to wait some time in the program, I'm just going to do import time so
that we can do Time.sleep.
Great.
So that's all for the import.
Now we can initialize the LED.
So once again let's keep things simple.
Let's create an LED variable is equal to LED uppercase.
And then in the parentheses we specify the Gpio which is still 17.
Okay we haven't changed the circuit.
So now the LED is initialized.
And we're going to ask the user input.
So I just put this here.
Okay.
You can see I just add some spaces.
It's just for the readability of the program.
Okay.
You could add zero spaces, but then it's going to be a bit hard to read.
Great.
And what do I do with this user choice.
Well here I'm going to use an if.
And I'm going to do if user choice is equal to zero.
And so pay attention here is not just one equal sign.
It's two equal sign.
Okay.
So once again one equal sign is used to assign a value from the right to the left.
But two equal sign is going to compare two values.
So for example with zero and then a colon.
So it's going to compare those two values and return true or false.
Okay.
So true if they are equal and false if they are not equal.
So if the user choice is equal with two equal to zero what do we want to do.
Well we want to turn off the LED so we can do LED, sort of as simple as that.
Then I go back to a new line and that's it for this block of code.
We don't want to do anything else.
So I'm going to go back to the previous indentation and add an L if okay.
So if the user choice this time is equal.
So user choice is equal with double equal to one.
Then I put a colon.
We go back to a new line with an indentation LED dot on.
So I turn on the LED.
So you might think well that's it.
But we also want to validate the input.
Which means that if the value is not correct we want to also say, well the choice is an invalid choice.
It must be 0 or 1.
So I'm going to go back to a new line again previous indentation.
And I can just put else.
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
an indentation and I can put for example, print.
Invalid choice must be 0 or 1.
You can also decide to print the choice if you want.
In this case, make sure that you cast this as a string, because here it's an integer.
So we print that.
And then I'm also going to add exit here.
And I will tell you why in just one minute.
Because what we want to do next is that after we turn off or we turn on the LED, we just want to wait
for two seconds.
So what I could do is to do time dot sleep with two and then time to sleep with two here, etc. but
I'm just going to put the time.sleep after this if like this time.sleep with two.
Which means that if we turn off or if we turn on, the LED is going to wait for two seconds anyway,
so we just need to write Time.sleep only once.
And in this case I have added the exit keyword here because if the choice is invalid, we don't want
to wait for two seconds.
So in this case we just exit from this point.
Okay, so to recap we first do all the imports that we need.
We initialize the LED.
We get the user input that we cast as an int.
And then we check if it's zero we power it off.
So we power off the LED.
If it's one we power on the LED.
And also so after this, after either this instruction or that instruction, we're going to go out of
the if and wait for two seconds.
But still in the if if the user choice is not 0 or 1, we print an error message.
So error message is basically just a normal message.
And then we also exit from the program.
So we don't execute that line of code.
And maybe I can just print end here.
So it's going to be even easier to see when the program ends.
All right.
So let's start this.
And as you can see when you initialize the LED it's going to be turned off by default.
And we have enter 0 or 1 here I'm just going to go with one.
For example I press enter.
You see the LED is turned on and after two seconds we have end.
We can start again.
And then I will say zero.
So actually the LED is already powered off.
But if I do zero well you see the LED is still powered off.
And then we have end.
And let's test the last case.
Let's say I give a wrong value.
So three in this case you see invalid choice must be 0 or 1.
And actually here you see we still have the end.
We still have the time.sleep because it's my mistake.
I gave you the exit keyword.
It's actually the exit function.
Okay.
You can see it can be very different.
He exit is a real keyword, but it's not going to do the same thing as the function.
So the function is going to actually exit from the program.
And to call the function you have to use parentheses.
Okay.
Let's try again.
I'm going to give three again.
Okay.
And you can see we have.
So we even have another message here.
Process ended with exit code zero.
And this time we didn't wait for two seconds and we don't print the end.
All right.
And I'm going to leave this mistake here in the video just to show you that as you can see, sometimes
things will work.
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
maybe you forgot some parentheses, or maybe you added extra parentheses that you shouldn't have.
Or you can see there can be a lot of different things like that that will give you a behavior that you
don't want.
So pay attention to everything you type, okay.
And double check your code before you run it.
All right.
And that's the end of this activity.