Let's now improve that if structure. So what
we've seen, we can execute this block of code if
that condition is True. But what if you want to
execute a different set of instruction, if the
condition is False. So you want to test if it's
True, execute something. And if it's False, you
execute something else. Well, let's see that. So
first, I'm going to come back to the first
statement here to make it very simple. Okay,
user_age greater or equal than 18, You're an
adult. And then I'm going to come back. So I press
Enter, and I go back to the first indentation and
I write else with a colon. I press Enter, I have a
new indentation, print, You're not an adult yet. So
in this case, so first it's going to check if that
is True. If that is True, it is going to execute
the next indented block of code. And if that is
not True, so if this is False, we go to the else.
And then we can execute that block of code. So now
let's run this. Okay, so we still have the Summer
time, End of program, but you have You're an adult,
because user_age is 30. Now you're gonna need to
put 17, and you can see You're not an adult yet.
So you can use if, and else, and you can also add
intermediate conditions with else if, for in Python,
it's named elif. And let's see that, at this
structure here. So I'm first going to modify this,
okay, because Summer time is actually, so from the
north hemisphere, it's June, July and August.
Okay. So let's say if month in June, July,
[Author typing]
August. So we use the test here to check if one
value is in one list. Okay. So if the value is
June or July or August its going to work and print
Summer time. Let's just test that. Okay, Summer time.
Now, what if I want to test if the month is
actually in the Winter time or the Spring time or
the Autumn time. What I can do, I can use else,
okay, not else if, okay, else if can be in other
languages. But for Python, this is going to be elif
month in let's say, we want to test September, and
then October, then November, we can print Autumn
[Author typing]
time. So what's going to happen is first is going
to check if this condition is True. If yes, it's
going to execute that block of code and then exit
Okay, once the condition is True, a block of code
is executed and then the if will exit. If this
is not True, is going to go to the next elif if
there is one and check if that is True. If that is
True, it is going to execute that. And if it's
False, it's not going to execute anything and then
any of the block here will not be executed and we
continue the execution here. Okay, now I'm going
to add another elif month in, so let's do all
here, so November, we have December, and then
January, and then February. If the month is in that
range, we are going to print Winter time and then
so here we still have three months in the year
okay, we have March, April, and May. I could do if
month in March, April, May, but I can just do else
because well that's the only possibility that's
left. So else print Spring time. We first check if
that is True, we print Summer time, we exit. If that
is False, we check this if, so if this is True, we
print Autumn time and exit. If this is still
False, we continue with this and if this is True,
we print Winter time. If this is still False, we go
to the else and then we print Spring time. Okay, so we
have July which is Summer time. Let's test with
September. You can see Autumn time, okay because
this is False. This is True. So we execute that
and then we skip all the rest. Now let's say May.
[No audio]
You can see Spring time okay, we are in the else.
So you can test here with different values okay to
see where you end up in a structure. All right now
you are able to use a structure to choose what to
execute depending on different conditions. This
will allow you to make your programs much more dynamic.