Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,860 --> 00:00:07,160
Now in the previous lecture, we learned what functions are, how to define a function and how to call
2
00:00:07,160 --> 00:00:07,610
it.
3
00:00:08,210 --> 00:00:14,930
We also learned that functions can take input like the one that we just created and they can return
4
00:00:14,930 --> 00:00:15,560
input.
5
00:00:15,740 --> 00:00:21,890
But as you can see, the one that we created here, the change Mac function does not return anything
6
00:00:21,890 --> 00:00:23,570
because it doesn't need to.
7
00:00:23,810 --> 00:00:29,960
It only takes interface and a new Mac and it changes the Mac address for the interface.
8
00:00:30,740 --> 00:00:35,310
So in this lecture, let's continue organizing our code.
9
00:00:35,330 --> 00:00:37,220
We're going to create a new function.
10
00:00:37,220 --> 00:00:43,610
And as we do that, we'll also have an example of a function that returns a value.
11
00:00:44,240 --> 00:00:52,460
So first of all, I just want to make this tidier here, and we're going to create our new function
12
00:00:52,460 --> 00:00:58,760
here and we're going to call this new function, get arguments.
13
00:01:00,870 --> 00:01:09,330
Because I want this function to parse the user input and return to me the arguments and the values entered
14
00:01:09,330 --> 00:01:10,320
by the user.
15
00:01:10,440 --> 00:01:13,690
So it's basically, what's this code in here?
16
00:01:13,710 --> 00:01:14,280
Does.
17
00:01:15,760 --> 00:01:19,060
So all we have to do is just copy all of this.
18
00:01:20,670 --> 00:01:27,600
And paste it in my function, but make sure that there is a tab, there is a distance in here before
19
00:01:27,600 --> 00:01:33,090
we paste this so that all of this will be considered as part of the function.
20
00:01:33,720 --> 00:01:36,960
Now I'm going to just delete these blank lines here.
21
00:01:38,480 --> 00:01:39,200
And that's it.
22
00:01:39,200 --> 00:01:43,280
We have our parser, so we have a function called get arguments.
23
00:01:43,310 --> 00:01:47,120
What it does is it creates our parser object.
24
00:01:47,270 --> 00:01:49,600
It adds an option of the interface.
25
00:01:49,610 --> 00:01:51,400
It adds an option of the mark.
26
00:01:51,410 --> 00:01:59,030
And finally it's passing the user input using parser to parse args and storing the values in options
27
00:01:59,030 --> 00:02:00,080
and arguments.
28
00:02:00,620 --> 00:02:07,250
The only problem is options and arguments are only accessible within this function right here.
29
00:02:07,250 --> 00:02:10,370
They're only accessible within this block of code.
30
00:02:10,910 --> 00:02:18,170
So even if we call this function in here at the bottom, the same way that we called our change mark.
31
00:02:19,180 --> 00:02:25,210
You'll see that pie chart is still telling us that options is an unresolved reference.
32
00:02:25,360 --> 00:02:32,500
It's still not going to know what options is because it's only used within the function in here.
33
00:02:33,040 --> 00:02:34,420
Let me show you what I mean.
34
00:02:34,420 --> 00:02:39,280
So I'm going to go down on my program and I'm just going to run the exact same command that we always
35
00:02:39,280 --> 00:02:42,970
run, and you'll see that we're going to get an error.
36
00:02:43,090 --> 00:02:49,180
And it's a good idea to learn how to read these errors and understand what's going wrong, because you'll
37
00:02:49,180 --> 00:02:51,160
definitely get errors in the future.
38
00:02:51,640 --> 00:02:57,010
So you can see that it's telling us that the error is in a file called Mac Changer Dot Pie.
39
00:02:57,070 --> 00:02:59,740
So this is the file that contains my code.
40
00:03:00,280 --> 00:03:08,080
The error is on line 22 and it's listing the line that contains the error and it's given us the error
41
00:03:08,080 --> 00:03:09,730
type, which is a name error.
42
00:03:09,970 --> 00:03:15,070
And the problem is the name options is not defined.
43
00:03:15,250 --> 00:03:23,860
So going up for line 22, we can see that it's even underlined in red here because Python doesn't know
44
00:03:23,860 --> 00:03:25,150
what options is.
45
00:03:25,150 --> 00:03:26,920
We never used it in here.
46
00:03:27,520 --> 00:03:31,750
Each block of code here is separate than the main program.
47
00:03:31,990 --> 00:03:37,870
Even though we're calling get arguments and even though we're setting options and arguments to equal
48
00:03:37,870 --> 00:03:44,800
to the parser dot parse args, this variable is only usable within this block of code.
49
00:03:45,700 --> 00:03:52,900
So in order to capture the value that's in here, what we need to do is I'm going to delete all of this.
50
00:03:52,900 --> 00:03:59,590
I'm actually going to cut it, not delete it, and we're going to say return parser to parse ARGs.
51
00:03:59,590 --> 00:04:08,590
So whenever you get called I want you to execute all the code in here and return parser dot parse args
52
00:04:08,590 --> 00:04:12,190
value to the location where you get called from.
53
00:04:12,430 --> 00:04:19,750
So we're calling all of this on line 21 in here and because we're using this return instruction in here,
54
00:04:20,230 --> 00:04:27,610
get arguments now will return whatever parser dot parse args returns and we can capture it the exact
55
00:04:27,610 --> 00:04:35,230
same way that we capture parser dot parse args by doing options arguments equals get arguments.
56
00:04:36,450 --> 00:04:36,960
Okay.
57
00:04:36,960 --> 00:04:44,520
So all we did is we copied all the code that we used to use to parse the arguments inside the function
58
00:04:44,520 --> 00:04:46,380
called get arguments.
59
00:04:46,890 --> 00:04:53,250
And instead of initializing a variable at the end, we returned the value that the parser that parse
60
00:04:53,250 --> 00:04:54,480
ARGs returns.
61
00:04:54,840 --> 00:05:02,310
Then whenever we call this function, we capture the returned value here using other variables.
62
00:05:02,580 --> 00:05:03,960
So we're using options.
63
00:05:03,960 --> 00:05:08,940
Arguments equals this and then again options is being called here.
64
00:05:08,940 --> 00:05:14,370
Now Python knows what options is equals to and it can run the change mac function.
65
00:05:15,840 --> 00:05:19,470
So here is what's going to happen when we execute the Python program.
66
00:05:19,740 --> 00:05:21,750
It's going to skip all of this.
67
00:05:21,930 --> 00:05:27,000
And the first thing that it's going to do is it's going to run the get argument function.
68
00:05:27,150 --> 00:05:31,710
The get argument function will read all the arguments entered by the user.
69
00:05:31,830 --> 00:05:33,630
It will pass them in here.
70
00:05:34,080 --> 00:05:40,440
And once done with that, it will return the two sets of information, the arguments and the values.
71
00:05:40,440 --> 00:05:46,140
Because we're using the return instruction, whatever that's being returned with, this function is
72
00:05:46,140 --> 00:05:48,960
being captured by options and arguments.
73
00:05:49,230 --> 00:05:51,900
Then we're calling the change mark.
74
00:05:52,020 --> 00:05:54,330
So Python is going to go here.
75
00:05:54,330 --> 00:06:00,300
We're giving it the interface, we're giving it the mark, and then it will execute all of these commands,
76
00:06:00,300 --> 00:06:02,610
which will change my Mac address.
77
00:06:03,060 --> 00:06:04,980
Now let's test this code.
78
00:06:05,830 --> 00:06:07,230
Using the same command.
79
00:06:07,240 --> 00:06:12,250
Let's just set the Mac address to be three three at the end.
80
00:06:12,490 --> 00:06:13,720
Hit enter.
81
00:06:13,720 --> 00:06:15,850
Get the right print message.
82
00:06:15,850 --> 00:06:23,110
And if we do, if config at zero, you'll see that my mac address did actually get changed.
83
00:06:23,620 --> 00:06:29,620
Now, if we look at our code right now, it looks much, much nicer, much more readable.
84
00:06:29,620 --> 00:06:35,650
And it's also reusable because we can reuse any of the functions whenever we want.
85
00:06:35,920 --> 00:06:40,780
So now we have the code for the main program in here, and that's only two lines.
86
00:06:40,780 --> 00:06:46,090
And then all the functionality is encapsulated into two functions.
87
00:06:46,090 --> 00:06:52,780
One of them gets the arguments entered by the user, and the other one changes the MAC address based
88
00:06:52,780 --> 00:06:55,000
on the argument that the user entered.
8926
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.