So in the previous video,
we briefly discussed the concepts of algorithms as
well as the if else else if statements,
now we're gonna put it into action. We're actually going to write a program
that will request a number between 1 to 9 from the user.
Now, if the number is greater than four, we are going to say
why so much?
Meaning that if it is four or less,
we're going to say why? So little.
Now the very last line I have blurred it for
a particular reason because once we write this first program,
we're going to
add an extra condition.
I don't want to show you what that extra condition is just yet.
So
let's focus on writing this program. All right. So let me just drag this away.
All right. And let me just remove this.
OK.
So the first part of the program is we want to request a number between 1 to 9. So
what can we do here?
Well, first of all,
we can create a variable called N,
all right, for the number
and we're going to pass, remember we worked with the input function.
So I'm gonna say input because that's the
function that would request an input from the user.
So I'm gonna say input and I'm going to say,
enter a number between 1
to 9.
OK.
Let's just, first of all, make sure that this is working. OK.
Enter number between 1 to 9.
Let me just create some space
between the one and nine
run again. OK.
All right.
So the
first line works.
Now
we want to check if the number
is between
or is less than a four, right? So
I'm going to say if
all right,
and then
the integer because we want to convert
whatever number we provided, we wanna convert it to an integer because
the user could be very naughty.
They could decide to provide, say 1.14 0.5 you know, 7.6 something like that. So
we want to keep everything as an integer. So I'm gonna wrap our variable right now
in the integer function,
right? So
I'm going to say if integer N,
OK? And now space and then the symbol for greater than
greater than four.
OK? And now pay very close attention. I'm going to add the double colon. OK?
You need to add this
whenever you commit your el statements. OK? So
I'm going to say that.
And now
by default, actually, Python has written out this line for me,
but I'm going to say
why so much.
OK?
Now enter
and now
I'm going to say
else
if
however,
OK.
And now I'm going to say
else
and then new line
indent print
and then I'm going to say,
uh why
so
little?
Ok.
So I'm gonna go ahead right now.
Oh, by the way, make sure in your LS you also have the double colon as well. OK.
Let's go ahead right now
and run the program
and let's see. So
enter number between 1 to 9, I'm going to say six
press enter,
you can see right there. It actually works. Why so much?
Let's run the program again. I'm going to say three,
three right there.
And then it says why so little? So
it actually works as you can see.
Now, I'm gonna go over this once again. OK? So first of all,
we have and which is going to be the variable that will accept the input from the user.
And the input is a question, enter a number between 1 to 9. OK.
So now we're gonna create a condition to that
will check to see the value of that number.
So
we have decided
to convert the input to an integer.
That's why we have the I NTA a question you might have here is wait a minute,
Alex Previously, when we had
a function inside of another function, we have to use the brackets, right?
The if else this is not exactly a function.
OK?
It's more of an algorithm,
more of kind of like a loop just checking to see the value of something.
It's not
an actual function itself. That's why
we don't have the integer wrapped in brackets. OK? So
it's gonna check if the integer and if it's greater than four will print,
why so much else?
Meaning that if the number
is four or less than four, print, why so little you can see it works, right? And
the only thing you need to pay attention to would be the double colon after
the four and also after the LS.
And then it's also very good practice that whenever you're working with,
if L statements or else if you have good indentation,
I didn't do this. OK? I didn't have my print statements
directly underneath the E and L statements. This is, this is not good at all.
This is very, very poor
programming syntax. So
you always want to have your indentation
correctly. OK?
Have that whenever you're working with the eve
statements.
OK?
Now
I'm gonna bring back
our program
and look at that. I have added an extra condition saying
that what if the number is a specific number which is five?
Well,
we want to say
bingo.
OK.
So how exactly are we going to do this? OK.
Let me track this away.
OK.
Now check this out.
I'm gonna come over here where it says print, why so much?
And I'm going to press enter because we want to add
the else if in between the open if statement and the
closing L statement.
So
now I'm going to say
L
if
this is the short form of writing, else, if so
else if we use in
Python,
so if the integer
and
is equal or
the integer N,
if it's equal
to five,
the equal is going to be double equal sign, not just one. OK?
So
else if, if the internet provided by the user is five
and now I'm going to add the double colon
and then I'm going to say
sorry, I'm gonna say print
then in brackets
bingo.
OK.
So I'm gonna go ahead right now.
We're on the program.
And now let's check to see if this actually works.
First of all, I'm going to type in three. OK.
OK. So it's still working. Why so little?
OK?
Uh Let's try eight
by so much. OK?
And now if I say five and I press enter,
guess what?
It's not saying Bingo,
it's still saying why so much?
You wanna know why?
Because the Python program
executes line by line. Now look at the hierarchy in here. The very first condition
that Python is checking for is to see if the number is greater than four
and because five is greater than four,
it immediately says, OK, why so much? It doesn't even
go further down
to check if the number is equal to five.
Because the very first statement is basically saying, hey,
if the number is greater than four, then print, why so much.
So to prove this to you,
what I'm gonna do here is this OK?
I'm going to say
that
let's actually change the hierarchy in here. So
we will check first of all,
if the integer is actually equal to five.
OK? Let that be the very first thing it checks.
And now over here,
we will check to see if the integer
is greater
than four.
So I've basically just changed the hierarchy.
So the very first thing I was gonna check right
now is if the integer is actually equal to five,
it's going to say uh bingo.
Else if, if it's
not equal to five
and it's greater than four,
we're going to say why so much.
And then else if it's
not equal to five and if it's not greater than four, then print why so little?
OK,
let's run the program. So I'm going to come over here right now. Run,
let me first of all, add five now and see. Enter
and there you go. Bingo. It actually worked.
Let's run the program again. What if I typed in to
enter? Why? So little? Once again, let's go with seven. Enter. Why? So much?
So you can see right now
it works.
So the hierarchy of your commands is extremely important
whenever you're working with Python or programming in general,
when you're checking for conditions, always keep in mind
that once Python
has gotten a condition that satisfied,
it will immediately print out the result, it's not going to go any further down
the remaining lines in your program. So
the hierarchy of your conditions,
the conditions of that check your commands is so so important.
And then of course, also this little
knowing where to add the
coons, things like that. This is something you will eventually get used to.
It might be a bit overwhelming in the very beginning.
But as you begin to write more programs,
you will eventually grow accustomed
to them.
So that's it for working with the if else, if statements, thank you for watching,
I will see you in the next class.