WEBVTT 00:00:00.210 --> 00:00:01.290 - [Instructor] Let's design a program 00:00:01.290 --> 00:00:03.930 that imports functionality from another file. 00:00:03.930 --> 00:00:06.090 When programming teams collaborate on projects, 00:00:06.090 --> 00:00:08.820 they're often writing code across multiple files. 00:00:08.820 --> 00:00:10.187 They package their work into functions 00:00:10.187 --> 00:00:13.050 and then share them for other team members to use. 00:00:13.050 --> 00:00:15.360 This is just another form of a module. 00:00:15.360 --> 00:00:16.839 Here, my teammate wrote a module 00:00:16.839 --> 00:00:19.470 for simulating a robot's movement. 00:00:19.470 --> 00:00:21.900 We can open up the file and take a look at the code, 00:00:21.900 --> 00:00:24.090 but we may not always have the background knowledge 00:00:24.090 --> 00:00:27.510 or time to understand the details of how it works. 00:00:27.510 --> 00:00:29.520 The good news is, that doesn't matter. 00:00:29.520 --> 00:00:30.651 The only thing I need to understand 00:00:30.651 --> 00:00:33.750 is the documentation of how to use it. 00:00:33.750 --> 00:00:34.980 If you notice up here, 00:00:34.980 --> 00:00:38.430 we've been writing our code in a file called main.py. 00:00:38.430 --> 00:00:41.040 The .py is the file extension for Python code, 00:00:41.040 --> 00:00:42.900 just like jpeg is for images. 00:00:42.900 --> 00:00:44.550 Then by convention in Python, 00:00:44.550 --> 00:00:47.130 we name the file with our main logic, main. 00:00:47.130 --> 00:00:49.785 In the Khan Academy IDE, when we press the run button, 00:00:49.785 --> 00:00:52.470 it runs the code in main.py. 00:00:52.470 --> 00:00:56.040 However, we can add other files or modules to our program, 00:00:56.040 --> 00:00:59.430 then import them into main.py and use their functions there. 00:00:59.430 --> 00:01:01.770 So if we go back to our main.py file, 00:01:01.770 --> 00:01:02.963 we can import the robot module 00:01:02.963 --> 00:01:05.587 and then call one of these functions. 00:01:05.587 --> 00:01:07.020 Okay, so let's start 00:01:07.020 --> 00:01:09.030 with the first function in the robot module, 00:01:09.030 --> 00:01:10.560 which is called reverse. 00:01:10.560 --> 00:01:13.560 It takes one input or argument, the direction, 00:01:13.560 --> 00:01:15.060 and from the doc string here, 00:01:15.060 --> 00:01:17.398 I see that it rotates the robot 180 degrees 00:01:17.398 --> 00:01:19.680 and returns its new direction. 00:01:19.680 --> 00:01:23.219 It also says the direction can either be left or right. 00:01:23.219 --> 00:01:25.500 So if I go back to main.py, 00:01:25.500 --> 00:01:26.580 I can call this function 00:01:26.580 --> 00:01:29.700 using the module name, robot.reverse. 00:01:29.700 --> 00:01:32.040 It's argument can be the string left or right. 00:01:32.040 --> 00:01:33.780 So let's say left for now, 00:01:33.780 --> 00:01:36.060 and then we know it returns the new direction. 00:01:36.060 --> 00:01:39.780 So I wanna store that value in a variable. 00:01:39.780 --> 00:01:42.000 Let's run it and see what it does. 00:01:42.000 --> 00:01:42.833 Nice. 00:01:42.833 --> 00:01:44.430 So this function is returning right, 00:01:44.430 --> 00:01:46.650 because it's reversing left. 00:01:46.650 --> 00:01:48.572 And then if I swap that and I try and reverse right, 00:01:48.572 --> 00:01:50.436 it returns left. 00:01:50.436 --> 00:01:52.740 Okay, not that interesting by itself. 00:01:52.740 --> 00:01:54.930 Let's see what this draw function does. 00:01:54.930 --> 00:01:57.090 It takes three inputs, the position, direction, 00:01:57.090 --> 00:01:59.750 and grid size, and it says it displays the robot's position 00:01:59.750 --> 00:02:01.749 and direction on the grid. 00:02:01.749 --> 00:02:04.140 Note that it doesn't say that it returned something, 00:02:04.140 --> 00:02:08.635 so it might just display and not return an output value. 00:02:08.635 --> 00:02:12.180 Back to main.py, let's call robot.draw. 00:02:12.180 --> 00:02:14.550 And we wanna give it a position, I don't know, 00:02:14.550 --> 00:02:16.980 let's say three, and then let's pass in the direction 00:02:16.980 --> 00:02:18.720 we got back from reverse. 00:02:18.720 --> 00:02:20.160 And it needs a grid size, 00:02:20.160 --> 00:02:22.680 which I guess needs to be bigger than the position. 00:02:22.680 --> 00:02:24.022 So let's say 10. 00:02:24.022 --> 00:02:25.560 Okay, nice. 00:02:25.560 --> 00:02:27.540 It looks like it's printing a little representation 00:02:27.540 --> 00:02:29.321 of the robot, and we can see that the arrow is pointing left 00:02:29.321 --> 00:02:31.912 because the robot is facing left. 00:02:31.912 --> 00:02:35.760 Let's draw the robot before we reversed it too. 00:02:35.760 --> 00:02:38.400 The position and grid size will be the same, 00:02:38.400 --> 00:02:39.660 but the direction will be right, 00:02:39.660 --> 00:02:42.090 so let's actually store that in the variable up here. 00:02:42.090 --> 00:02:44.490 And actually let's store the position and grid size 00:02:44.490 --> 00:02:45.390 in a variable as well, 00:02:45.390 --> 00:02:47.673 so we're not repeating three, three, 10, 10. 00:02:47.673 --> 00:02:51.930 Note that I'm not making any changes to the robot.py file. 00:02:51.930 --> 00:02:53.640 I'm just using this module. 00:02:53.640 --> 00:02:56.087 All of my code is going in main.py. 00:02:56.087 --> 00:02:58.920 Let's try one more function, move forward. 00:02:58.920 --> 00:03:01.860 We see that it takes a position, direction, and grid size, 00:03:01.860 --> 00:03:04.380 and it returns the robot's new position. 00:03:04.380 --> 00:03:07.440 So back to main.py, we call robot.moveforward 00:03:07.440 --> 00:03:08.970 and we pass in the robot position, 00:03:08.970 --> 00:03:10.770 the direction, and the size. 00:03:10.770 --> 00:03:12.360 And since it returns the new position, 00:03:12.360 --> 00:03:14.362 we're going to store the result of that function call 00:03:14.362 --> 00:03:16.890 in the robot position variable. 00:03:16.890 --> 00:03:17.884 And so we can see what happens. 00:03:17.884 --> 00:03:20.251 Let's call draw afterward. 00:03:20.251 --> 00:03:22.530 Now that we've experimented, 00:03:22.530 --> 00:03:24.750 we've got a good sense for how the robot module works. 00:03:24.750 --> 00:03:27.990 So let's try building this into a more interesting program. 00:03:27.990 --> 00:03:30.060 Maybe we want the robot to randomly decide 00:03:30.060 --> 00:03:32.820 whether it moves forward or reverses at any point. 00:03:32.820 --> 00:03:34.590 So I could generate a random number, 00:03:34.590 --> 00:03:37.560 and depending on the result, decide which function to call. 00:03:37.560 --> 00:03:39.660 That means I'm gonna need the random module. 00:03:39.660 --> 00:03:42.073 By convention, usually we alphabetize our imports, 00:03:42.073 --> 00:03:44.022 so I'm gonna put random first. 00:03:44.022 --> 00:03:45.949 And because there are two possible outcomes, 00:03:45.949 --> 00:03:49.110 I'll generate a random number between one and two. 00:03:49.110 --> 00:03:51.300 If I get a one, I'll have the robot move forward, 00:03:51.300 --> 00:03:53.637 and if I get a two, I'll have it reverse. 00:03:53.637 --> 00:03:56.970 Now I wanna generate three random numbers, 00:03:56.970 --> 00:03:59.550 because I want the robot to take three random actions. 00:03:59.550 --> 00:04:00.383 I don't want it 00:04:00.383 --> 00:04:03.210 to perform the same one random action three times. 00:04:03.210 --> 00:04:06.570 So I need to have three separate randint function calls. 00:04:06.570 --> 00:04:08.610 In fact, I'll just copy this whole block of code, 00:04:08.610 --> 00:04:11.149 because I want all of this functionality to repeat. 00:04:11.149 --> 00:04:13.500 Now, if I run the program a few times, 00:04:13.500 --> 00:04:15.749 I can see that the robot's moving randomly. 00:04:15.749 --> 00:04:16.860 One last thing. 00:04:16.860 --> 00:04:19.380 Maybe let's have the robot start at a random position 00:04:19.380 --> 00:04:20.640 each time too. 00:04:20.640 --> 00:04:22.290 The robot's gotta start on the grid. 00:04:22.290 --> 00:04:24.030 So the start should be one, 00:04:24.030 --> 00:04:27.150 and the stop should be the grid size. 00:04:27.150 --> 00:04:28.470 And now when I run the program, 00:04:28.470 --> 00:04:31.486 the robot's starting at a random position each time. 00:04:31.486 --> 00:04:33.750 Pretty cool how quickly we were able 00:04:33.750 --> 00:04:36.420 to get a complex program like that up and running. 00:04:36.420 --> 00:04:38.364 That's the power of using modules. 00:04:38.364 --> 00:04:40.950 We can build off a functionality other people 00:04:40.950 --> 00:04:42.898 have already written to expand the possibilities 00:04:42.898 --> 00:04:44.193 of our programs.