- In this video, we're going to be taking
a look at the "if" function,
because sometimes we might want
to do something if something else is true.
And I've got my interactive interpreter,
the idle environment running here
on the right hand side of the screen,
and I've already gone under file
and said new file,
and I've got my file editor on the left of the screen,
and I've named the file evaluation.
Now let's get started by entering the following commands,
and what we're going to be doing here is,
we're going to be prompting the user to enter
the native VLAN, the voice VLAN
and the data VLAN configuration
we might have on a sisco catalyst switch
for a voice over IP environment
where we're using a sisco IP phones
and sisco has the best practice recommendations
that the native VLAN equal the data VLAN,
and we're going to be checking to see if that's the case,
and if not, we want to warn the user about that.
So let's ask them what VLANs their using.
I'm going to say native VLAN,
using our input statement again.
I'll say input and in parentheses
and in quotes, I'm going to say,
"What is your native VLAN?"
Question mark, space, close quote, close parentheses.
Let's ask about the voice VLAN.
I'll say voice VLAN input,
what is your voice VLAN?
Question mark, space, close quote, close parentheses,
and lets do the data VLAN.
I'll say data VLAN equals input.
What is your data VLAN?
Question mark, space, close quote, close parentheses.
So that's going to populate our variables.
Now lets do that evaluation using the if function.
I'm going to say if the native VLAN equals,
and remember the best practice
is that the native VLAN equal the equal the data VLAN.
I'll say if it equals,
but remember when we're doing a boolean test,
we don't just say equals,
because that's how we assign a variable.
We need to use a double equal sign.
So I'll say if native VLAN equal equal the data VLAN,
and I'm going to give a colon to go down
and say what we are going to do if those two do in fact equal.
I'll say colon,
and notice it indented a little bit here,
and I'm going to say if those two do in fact equal,
then I want to print a message that says looks good.
I'll say print and in parentheses and quotes,
I'll say looks good, exclamation point,
close quote, close parentheses.
But what if those two do not equal?
I'm going to backspace and I'm going to say else colon,
so those do not equal,
what are we going to do?
We're going to print a different message.
I'll say we're going to print the message,
parentheses, quote.
Caution:
It's a best practice
for the native VLAN
and data VLAN
to be the same.
Close quote, close parentheses.
Now lets save this again and run it.
I'll say save then I'll say run it.
It says, "What is your native VLAN?"
I'll say the native VLAN is one hundred,
I'll say my voice VLAN is two hundred,
and we'll say that I am adhering to best practices
and my data VLAN equals my native VLAN of one hundred,
and it says, "Looks good!"
What if those did not equal though?
Let's just test that.
I'll say in this case,
my native VLAN is one,
my voice VLAN is two hundred,
and my data VLAN is one hundred.
Then it gives me the caution
that it's the best practice for the native VLAN
and the data VLAN to be the same.
Something else we could do
is a compound evaluation with our boolean logic.
In other words, we can say,
I want to do something if this condition is true
and this other condition is true.
For example, let's have the user
enter their CoS and DSCP values
for their voice packets,
and we're going to check to see if
they're adhering to best practices.
It's a best practice that the CoS of a voice packet be five,
and a DSCP value be forty-six.
So lets just update what we have here.
We'll create a variable called CoS
and I'm going to ask the user,
what is your voice CoS value.
We'll also ask them about their DSCP value.
What is your voice DSCP value?
And lets do one of those compound evaluations .
We're going to say if
the CoS value equals five,
and by the way this is going to be
a string value that we're evaluating,
so I need to put this in quotes.
I'll say if CoS equals five,
and, I want something else to be true,
and DSCP, double equal sign, equals forty-six,
and then I'll do a colon after that.
I'll say print looks good.
Else we want to give another caution.
I'll say it's a best practice for,
over write this,
it's a best practice for the voice CoS to be five
and the voice DSCP,
make this upper case.
The voice DSCP to be forty-six.
Close quote, close parentheses.
Let's save and run this.
We'll do a save.
Let's do a run module.
What is my voice CoS value?
Well let's say that we are using best practices,
and it's going to be a five,
and my DSCP value is forty-six.
It says, "Looks good!"
What if I'm not following best practices?
I can say my voice CoS is a three
and my voice DSCP value is a forty-six.
It's going to say,
ahh, your best practices is not being followed,
and it tells us what that best practice is.
And finally in this video,
let's look at another way of doing an evaluation.
We're going to use something called the elif function, E-L-I-F.
After we do the initial evaluation with the if statement,
we can do a subsequent evaluation.
We can say if this were true then do this,
otherwise let's check something else
and see if that's true,
and if it is, we'll do this.
And if neither of those are true,
then we'll do whatever's specified by the else statement.
So let's do one more modification here.
Let's say what is your voice DSCP value?
And if we have a DSCP value of a forty-six,
it's probably a voice media packet.
If it's a voice DSCP value of twenty-four,
it's probably a voice signaling packet.
And let's try to figure out which one it is.
We'll say if the DSCP value
equals forty-six,
then we'll print to the screen,
this is a voice media packet.
Now let's do a subsequent evaluation.
We'll say elif and we'll do another conditional test.
I'll say elif DSCP equals equals twenty-four end quotes.
And we have to put the colon afterwards.
If DSCP equals twenty-four but DSCP did not equal forty-six,
then we're going to print,
this is a voice
signaling packet.
And if that's not true,
then we'll say this is not a voice packet.
All right, let's save it and run it and see what happens.
What is my DSCP value?
Let's say it's forty-six.
It says, oh that's a voice media packet.
Let's run it again.
Let's say this time it's a twenty-four.
It's a voice signaling packet.
Let's run it one more time
and say that our value is forty-one.
It says this is not a voice packet.
And that's a look at how we can do evaluations
using the if function.
And we saw a couple of it's components.
The else and the elif functions.