Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Okay.
Let's put what we learned about if statements and practice and check if the user put a value for the
interface and for the MAC address.
Now from before we know that the options variable and here contains the values for the interface and
for the Mac address.
So all we have to do is check if options dot interface and options dot new Mac contain values.
We're going to have to do this check in here because the options is being captured in here.
I don't really like to do that because we have a get arguments function and we should do everything
that's related to the arguments within this function.
That's why what I'm going to do, I'm going to revert back to something that we had before when we actually
used to capture the arguments in here.
And what we're doing now is very similar to what we were doing before.
We're initializing the options and the arguments with whatever is being returned from the parser and
parse ARGs.
So the options is going to contain the values entered by the user and the arguments is going to contain
the arguments entered by the user.
Now we're going to use our if statement after that.
So following the syntax that I showed you in the previous lecture, we have to do first do the if statement
followed by the condition.
And like I said, there is a large number of ways to write conditions.
We can compare values.
For example, if we have variables, we can check if X is equal to one.
For example, we can check if something is greater than something.
Something is smaller than something if a list contains an element and so on.
In today's lecture, we're going to use something that's relevant to our goal, and we want to check
if options contains a value for the interface and if it contains a value for the Mac address.
So our first condition is going to be if options don't interface.
That's all we have to do.
So all we're saying is if options don't interface, then we put the call on and then we can execute
code and as you can see automatically py charm has indented the space for us and it's allowing us to
write code in here, which means this block of code will only be executed if this condition is true.
And our condition is if options dot interface.
So if options dot interface holds a value.
Now if you think of it, we don't want to check if options dot interface holds a value.
We want to check.
If it does not hold the value, then we want to show an error.
So what we're going to do is we're going to do, if not options dot interface, then we're going to
write code to handle error.
Now I'm putting this as a comment because I want to get back to it later.
Now, the next thing that we want to do is to check if the user entered a value for the Mac address.
Now we can we can do this using the leaf or using another if statement.
In our example, it doesn't really matter which one we use because we're going to exit in the first
condition if the user did not put a value for the interface.
But I'm going to go with Elif, which is short for LCF and we're going to say again if not.
Options dot new Mac.
And again in here, we're going to execute code.
To handle error.
Okay.
Now, I didn't put any code in here because I want to have a quick look on the statement and then we'll
put our code.
So what we're doing now is we're checking, if not options dot interface.
So if the user did not put a value for the interface, then we want to display an error and exit.
If they did, then we're going to move to the next check and we'll check if they did not enter a value
for the new Mac.
If they didn't, then we'll also print an error and exit.
Now you can just use a normal print statement and then use an exit statement in Python to exit.
Or we can actually use the parser object to handle this for us.
Because remember when we created the parser object here, we said this is an entity or a person that
knows everything about parsing and can handle all aspects of parsing.
So we can just do parser dot error.
And what this will do.
As you can see, it's already shown us how to use it.
It will basically display the message that we put and exit.
So the message that we're going to display to the person is going to say, please specify an interface,
use dash, dash, help for more info.
And again, we're going to use a similar instruction for this one.
The only difference is we're going to say, please specify a mark.
Use dash, dash, help for info.
Now this is all good.
One thing that we haven't done so far is handle the code after the if statement.
So after this if statement, if we reach this line, line 16, that means the user put an interface
and put a new mark.
So we should be able to change the mark for them.
So all we have to do at this point is return options.
And I chose the return options because we never use the arguments in our program.
So it makes sense to just return the options from the get arguments in here.
And then when we're calling get arguments here, we have to capture the value of options.
And I'm just going to create another variable and I'm going to call it options.
And that's it.
Now let's have a quick look at our program.
Up to this line, we know that options and arguments will contain the arguments and the values returned
by parser to parse ARGs, which is the values and the arguments entered by the user.
Then we're using an if statement in the first condition.
We'll check in if the interface is not set.
If it's not set, then we're going to display.
Please specify an interface and then exit the program.
If it is set, then this condition is false.
We're going to go to the next condition and we're going to check if the options dot new Mac is set.
If it's not set, then we're going to display all of this and exit.
So if we reach this line and we didn't exit the program, then that means we have an interface and we
have a new Mac.
The variable that holds the interface and the new Mac is the options.
So we can just return that.
We're capturing this in here where we are calling the get arguments and then we're using options that
interface and options the new mark in the change mark which will execute all of this and change the
Mac address for me.
Now let's go and run this program and see how it's going to handle the errors.
So, first of all, let's run it without a mac address.
And as you can see, it's given us the usage and it's given us the error message that we specified,
saying, please specify a new Mac, use help for more info.
Now let's try to give it a mac, but not give it an interface.
And again, we get an error message telling us, please specify an interface.
Now, if we run the program properly, just to make sure that it works and this time let's change the
Mac address to something that ends with one one hit enter.
We get a proper print statement and if we do, if config 88 zero, you'll see that the MAC address did
change to the MAC address that we want.
So that's it for now.
Quick example of using an if statement.
I highly recommend spending some time experimenting with different types and different styles of IF
statements.
Something similar to what I showed you in the previous lecture just to get more comfortable with it.
We're going to be using this a lot throughout the course.
We're going to be using a lot of if statements.
We're going to be using a lot of conditions.
So using this is going to become very easy for you anyway.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.