Hello everybody and welcome back to this tutorial. And in the previous video what
we saw is we coded a simple password checker. We also were introduced to the
if-else statement. And what we are going to do right now is we are going to see
what the if-else statement can do once we have multiple statements
that we want to specify. We're also going to write a simple example to demonstrate
that thing. So, the if else statement has something called else/if, which you can
add in between the if and in between the else statements. So I mean in between
those two. There can be as many else/if statements as you'd like, or as many as
you need, and then you can code them into your program. So let us see how that will
work. Open up your IDLE right here. We're going
to open a new file. Let us right away save it as thirdprogram.py. So save
as to the desktop, thirdprogram.py, and press here enter. And right now we
are going to code this simple program right here. There's no
need to actually enlarge this, let us code it like this. So let's think about
it. So, first of all, let us do something like this:
inputted_number, so inputted_number, will be our actual
variable, that will store a number, that the user will specify in the program
itself. So once they run the program we want to use the input function, and we
want to prompt the user to specify any number they like. So,
'Specify Any Number You Want: '. This will get prompted to the actual user. We can
then print the number if we want to, so print(inputted_number), and
right here we are going to get into the else/if statement. So, first of all, we
want to check if, so if inputted_number is
bigger than 0, so simply just specify it like this, what we want to print is
inputted number. And then we can concat
the string, so plus the string of inputted_number, and then plus another string
which will be, for example, is bigger then 0.
Let's also add a space right here so we don't actually concat the string
one right behind another one. So, basically, what we are printing right here is if
inputted_number is bigger than 0, we basically are going to print inputted
number plus the string of the inputted_number, which will just transform this
integer value right here to be a string. So, first of all, before we actually
convert it let us type it right here to be an integer, so inputted_number is
going to be equal to the integer of the inputted_number. Just in
case we are only going to use the string value of it in the print statements, and
we're going to print inputted_number then the inputted_number string is bigger than
0. And then we want to type here else for the second statement. So else it
is smaller the 0, of course. So, else: print inputted number. Basically, we want
to do the same thing, so plus string of the inputted_number itself, so inputted
number,
close the brackets, plus and then we want to write right here is smaller than 0.
Right here we do not need, or we can use these actual two dots. Let me just add it
right here as well. But you might notice, what happens if the user types 0? So
let us actually take a look before we actually code the rest of the program.
What would happen if the user specifies 0? So run module right here, save it, specify
any number you want. Let's first of all check it whether it works for the
numbers greater or lesser than 0, so 3. We print out the number itself, so
right here, and then we get Inputted Number 3 is bigger than 0, which is correct. Now
let's try it with a different number. So run, -3. Inputted number -3 is
smaller than 0. So our program works perfectly in these two cases. But, we also
have a third case where we actually can run the program, and the user can specify
simply 0. And it says right here, Inputted number 0 is smaller than 0. Now the
reason why it says this is because it is in the else statement. So what this
program basically did is it checked whether 0 is greater than 0, then it will
print this. In any other case, in basically any other case it doesn't have
to be the case where the actual number is smaller than 0, it also can be equal to 0
as in our case, it will print the inputted number is smaller than 0. But we also
want to add an else/if statement in between, which we can add just like this.
So else/if, simply just specify elif, it is shortened for else/if,
then you want to specify the statement. So elif
inputted_number == 0.
Then we want to print inputted number, and
then plus the actual string of that number, string of the inputted_number, and
then plus open single quotes is equal to 0. Basically, this should be the entire
program. Let us see how this will work. So we added another else/if statement that
will check if the inputted number is equal to 0. So let us save this, first of all,
so save, and then run it right here, and specify any number you want, we specify
0, and you can see inputted number 0 is equal to 0. You don't need to
actually do it like this if you don't want to. Basically, just simply delete
this right here. And for your own needs if you, for example, wanted to do
something like this, you can also use a simple if/else statement, but also add
right here an equal sign. So greater or equal to 0
then print inputted number + str(input_number) is bigger than 0 or
equal to 0. Let me just move this so we can see the entire program. And then
you can save this right here, and you can run it like this as well. Basically, you
will not have an else/if statement. What you will do is you will simply print out
once the number is greater or equal to zero, you will print inputted number is
bigger than 0 or equal to 0. Which really doesn't make any sense, the
previous version is a lot better, but if you need something that is in for any
case you want you can do it. So let us run it, specify -2, inputted number is
smaller than 0. Let us run it once again, so run module, then let's specify 3
is bigger or equal to 0. And also once we actually specify 0
it should print out the same thing, as we can see right here. So the inputted number
0 is bigger than 0 or equal to 0, which is actually kind of correct.
It is not bigger than 0 but it is however equal to 0. So that would be a
simple example of the else/if statement. We covered fully the if statement with
the if-else and with the else/if statements, so that part is finished. What
we are going to do in the next video is we are going to cover the while loop. So I
hope you enjoyed this tutorial and I hope I see you in the next lecture. Bye!