Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,000 --> 00:00:01,480
Okay.
2
00:00:01,480 --> 00:00:08,200
Let's put what we learned about if statements and practice and check if the user put a value for the
3
00:00:08,200 --> 00:00:10,660
interface and for the MAC address.
4
00:00:11,080 --> 00:00:18,220
Now from before we know that the options variable and here contains the values for the interface and
5
00:00:18,220 --> 00:00:19,930
for the Mac address.
6
00:00:20,320 --> 00:00:27,880
So all we have to do is check if options dot interface and options dot new Mac contain values.
7
00:00:28,240 --> 00:00:33,640
We're going to have to do this check in here because the options is being captured in here.
8
00:00:34,270 --> 00:00:41,230
I don't really like to do that because we have a get arguments function and we should do everything
9
00:00:41,230 --> 00:00:44,530
that's related to the arguments within this function.
10
00:00:45,580 --> 00:00:51,430
That's why what I'm going to do, I'm going to revert back to something that we had before when we actually
11
00:00:51,430 --> 00:00:55,450
used to capture the arguments in here.
12
00:00:56,880 --> 00:01:00,610
And what we're doing now is very similar to what we were doing before.
13
00:01:00,630 --> 00:01:06,810
We're initializing the options and the arguments with whatever is being returned from the parser and
14
00:01:06,810 --> 00:01:07,800
parse ARGs.
15
00:01:08,040 --> 00:01:14,490
So the options is going to contain the values entered by the user and the arguments is going to contain
16
00:01:14,490 --> 00:01:16,620
the arguments entered by the user.
17
00:01:17,310 --> 00:01:20,970
Now we're going to use our if statement after that.
18
00:01:21,180 --> 00:01:28,110
So following the syntax that I showed you in the previous lecture, we have to do first do the if statement
19
00:01:28,620 --> 00:01:30,300
followed by the condition.
20
00:01:30,300 --> 00:01:34,500
And like I said, there is a large number of ways to write conditions.
21
00:01:34,500 --> 00:01:35,910
We can compare values.
22
00:01:35,910 --> 00:01:40,290
For example, if we have variables, we can check if X is equal to one.
23
00:01:40,290 --> 00:01:44,120
For example, we can check if something is greater than something.
24
00:01:44,190 --> 00:01:50,310
Something is smaller than something if a list contains an element and so on.
25
00:01:50,790 --> 00:01:56,070
In today's lecture, we're going to use something that's relevant to our goal, and we want to check
26
00:01:56,070 --> 00:02:03,150
if options contains a value for the interface and if it contains a value for the Mac address.
27
00:02:03,480 --> 00:02:09,449
So our first condition is going to be if options don't interface.
28
00:02:10,740 --> 00:02:12,010
That's all we have to do.
29
00:02:12,030 --> 00:02:19,320
So all we're saying is if options don't interface, then we put the call on and then we can execute
30
00:02:19,320 --> 00:02:26,940
code and as you can see automatically py charm has indented the space for us and it's allowing us to
31
00:02:26,940 --> 00:02:33,960
write code in here, which means this block of code will only be executed if this condition is true.
32
00:02:34,260 --> 00:02:37,620
And our condition is if options dot interface.
33
00:02:37,620 --> 00:02:41,160
So if options dot interface holds a value.
34
00:02:42,150 --> 00:02:47,580
Now if you think of it, we don't want to check if options dot interface holds a value.
35
00:02:47,610 --> 00:02:48,780
We want to check.
36
00:02:48,780 --> 00:02:52,350
If it does not hold the value, then we want to show an error.
37
00:02:52,680 --> 00:02:59,460
So what we're going to do is we're going to do, if not options dot interface, then we're going to
38
00:02:59,460 --> 00:03:03,390
write code to handle error.
39
00:03:03,420 --> 00:03:06,840
Now I'm putting this as a comment because I want to get back to it later.
40
00:03:07,970 --> 00:03:14,300
Now, the next thing that we want to do is to check if the user entered a value for the Mac address.
41
00:03:14,900 --> 00:03:21,230
Now we can we can do this using the leaf or using another if statement.
42
00:03:22,370 --> 00:03:28,130
In our example, it doesn't really matter which one we use because we're going to exit in the first
43
00:03:28,130 --> 00:03:31,910
condition if the user did not put a value for the interface.
44
00:03:32,570 --> 00:03:40,070
But I'm going to go with Elif, which is short for LCF and we're going to say again if not.
45
00:03:41,080 --> 00:03:43,510
Options dot new Mac.
46
00:03:46,220 --> 00:03:49,370
And again in here, we're going to execute code.
47
00:03:50,640 --> 00:03:53,460
To handle error.
48
00:03:54,380 --> 00:03:54,880
Okay.
49
00:03:54,950 --> 00:04:00,320
Now, I didn't put any code in here because I want to have a quick look on the statement and then we'll
50
00:04:00,320 --> 00:04:01,550
put our code.
51
00:04:02,000 --> 00:04:07,280
So what we're doing now is we're checking, if not options dot interface.
52
00:04:07,280 --> 00:04:13,460
So if the user did not put a value for the interface, then we want to display an error and exit.
53
00:04:14,120 --> 00:04:20,690
If they did, then we're going to move to the next check and we'll check if they did not enter a value
54
00:04:20,690 --> 00:04:21,959
for the new Mac.
55
00:04:22,100 --> 00:04:25,970
If they didn't, then we'll also print an error and exit.
56
00:04:26,240 --> 00:04:32,120
Now you can just use a normal print statement and then use an exit statement in Python to exit.
57
00:04:32,660 --> 00:04:38,990
Or we can actually use the parser object to handle this for us.
58
00:04:39,440 --> 00:04:46,040
Because remember when we created the parser object here, we said this is an entity or a person that
59
00:04:46,040 --> 00:04:50,870
knows everything about parsing and can handle all aspects of parsing.
60
00:04:51,590 --> 00:04:54,560
So we can just do parser dot error.
61
00:04:56,830 --> 00:04:58,090
And what this will do.
62
00:04:58,090 --> 00:05:00,580
As you can see, it's already shown us how to use it.
63
00:05:00,880 --> 00:05:05,350
It will basically display the message that we put and exit.
64
00:05:05,620 --> 00:05:12,340
So the message that we're going to display to the person is going to say, please specify an interface,
65
00:05:12,340 --> 00:05:15,370
use dash, dash, help for more info.
66
00:05:16,710 --> 00:05:21,810
And again, we're going to use a similar instruction for this one.
67
00:05:21,840 --> 00:05:26,940
The only difference is we're going to say, please specify a mark.
68
00:05:27,060 --> 00:05:29,670
Use dash, dash, help for info.
69
00:05:30,300 --> 00:05:32,070
Now this is all good.
70
00:05:32,100 --> 00:05:38,110
One thing that we haven't done so far is handle the code after the if statement.
71
00:05:38,130 --> 00:05:45,870
So after this if statement, if we reach this line, line 16, that means the user put an interface
72
00:05:45,870 --> 00:05:47,210
and put a new mark.
73
00:05:47,220 --> 00:05:50,970
So we should be able to change the mark for them.
74
00:05:51,090 --> 00:05:56,340
So all we have to do at this point is return options.
75
00:05:56,910 --> 00:06:01,800
And I chose the return options because we never use the arguments in our program.
76
00:06:01,800 --> 00:06:06,480
So it makes sense to just return the options from the get arguments in here.
77
00:06:07,450 --> 00:06:13,270
And then when we're calling get arguments here, we have to capture the value of options.
78
00:06:13,270 --> 00:06:17,320
And I'm just going to create another variable and I'm going to call it options.
79
00:06:18,790 --> 00:06:19,600
And that's it.
80
00:06:19,930 --> 00:06:22,510
Now let's have a quick look at our program.
81
00:06:22,960 --> 00:06:29,860
Up to this line, we know that options and arguments will contain the arguments and the values returned
82
00:06:29,860 --> 00:06:35,920
by parser to parse ARGs, which is the values and the arguments entered by the user.
83
00:06:36,910 --> 00:06:40,660
Then we're using an if statement in the first condition.
84
00:06:40,660 --> 00:06:43,720
We'll check in if the interface is not set.
85
00:06:44,230 --> 00:06:47,500
If it's not set, then we're going to display.
86
00:06:47,500 --> 00:06:51,580
Please specify an interface and then exit the program.
87
00:06:52,060 --> 00:06:55,840
If it is set, then this condition is false.
88
00:06:55,840 --> 00:07:03,550
We're going to go to the next condition and we're going to check if the options dot new Mac is set.
89
00:07:03,880 --> 00:07:08,860
If it's not set, then we're going to display all of this and exit.
90
00:07:09,520 --> 00:07:16,750
So if we reach this line and we didn't exit the program, then that means we have an interface and we
91
00:07:16,750 --> 00:07:17,770
have a new Mac.
92
00:07:18,160 --> 00:07:23,440
The variable that holds the interface and the new Mac is the options.
93
00:07:23,440 --> 00:07:25,480
So we can just return that.
94
00:07:26,370 --> 00:07:32,070
We're capturing this in here where we are calling the get arguments and then we're using options that
95
00:07:32,070 --> 00:07:38,010
interface and options the new mark in the change mark which will execute all of this and change the
96
00:07:38,010 --> 00:07:39,210
Mac address for me.
97
00:07:40,190 --> 00:07:45,890
Now let's go and run this program and see how it's going to handle the errors.
98
00:07:46,130 --> 00:07:49,700
So, first of all, let's run it without a mac address.
99
00:07:50,240 --> 00:07:55,970
And as you can see, it's given us the usage and it's given us the error message that we specified,
100
00:07:55,970 --> 00:08:00,350
saying, please specify a new Mac, use help for more info.
101
00:08:00,890 --> 00:08:05,720
Now let's try to give it a mac, but not give it an interface.
102
00:08:08,600 --> 00:08:13,340
And again, we get an error message telling us, please specify an interface.
103
00:08:14,060 --> 00:08:20,090
Now, if we run the program properly, just to make sure that it works and this time let's change the
104
00:08:20,090 --> 00:08:23,840
Mac address to something that ends with one one hit enter.
105
00:08:23,870 --> 00:08:33,110
We get a proper print statement and if we do, if config 88 zero, you'll see that the MAC address did
106
00:08:33,110 --> 00:08:35,330
change to the MAC address that we want.
107
00:08:37,090 --> 00:08:38,380
So that's it for now.
108
00:08:38,409 --> 00:08:41,250
Quick example of using an if statement.
109
00:08:41,260 --> 00:08:48,000
I highly recommend spending some time experimenting with different types and different styles of IF
110
00:08:48,010 --> 00:08:48,910
statements.
111
00:08:48,910 --> 00:08:53,710
Something similar to what I showed you in the previous lecture just to get more comfortable with it.
112
00:08:53,830 --> 00:08:56,680
We're going to be using this a lot throughout the course.
113
00:08:56,680 --> 00:08:58,570
We're going to be using a lot of if statements.
114
00:08:58,570 --> 00:09:00,700
We're going to be using a lot of conditions.
115
00:09:00,700 --> 00:09:04,180
So using this is going to become very easy for you anyway.
11728
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.