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