1 00:00:00,210 --> 00:00:01,290 - [Instructor] Let's design a program 2 00:00:01,290 --> 00:00:03,930 that imports functionality from another file. 3 00:00:03,930 --> 00:00:06,090 When programming teams collaborate on projects, 4 00:00:06,090 --> 00:00:08,820 they're often writing code across multiple files. 5 00:00:08,820 --> 00:00:10,187 They package their work into functions 6 00:00:10,187 --> 00:00:13,050 and then share them for other team members to use. 7 00:00:13,050 --> 00:00:15,360 This is just another form of a module. 8 00:00:15,360 --> 00:00:16,839 Here, my teammate wrote a module 9 00:00:16,839 --> 00:00:19,470 for simulating a robot's movement. 10 00:00:19,470 --> 00:00:21,900 We can open up the file and take a look at the code, 11 00:00:21,900 --> 00:00:24,090 but we may not always have the background knowledge 12 00:00:24,090 --> 00:00:27,510 or time to understand the details of how it works. 13 00:00:27,510 --> 00:00:29,520 The good news is, that doesn't matter. 14 00:00:29,520 --> 00:00:30,651 The only thing I need to understand 15 00:00:30,651 --> 00:00:33,750 is the documentation of how to use it. 16 00:00:33,750 --> 00:00:34,980 If you notice up here, 17 00:00:34,980 --> 00:00:38,430 we've been writing our code in a file called main.py. 18 00:00:38,430 --> 00:00:41,040 The .py is the file extension for Python code, 19 00:00:41,040 --> 00:00:42,900 just like jpeg is for images. 20 00:00:42,900 --> 00:00:44,550 Then by convention in Python, 21 00:00:44,550 --> 00:00:47,130 we name the file with our main logic, main. 22 00:00:47,130 --> 00:00:49,785 In the Khan Academy IDE, when we press the run button, 23 00:00:49,785 --> 00:00:52,470 it runs the code in main.py. 24 00:00:52,470 --> 00:00:56,040 However, we can add other files or modules to our program, 25 00:00:56,040 --> 00:00:59,430 then import them into main.py and use their functions there. 26 00:00:59,430 --> 00:01:01,770 So if we go back to our main.py file, 27 00:01:01,770 --> 00:01:02,963 we can import the robot module 28 00:01:02,963 --> 00:01:05,587 and then call one of these functions. 29 00:01:05,587 --> 00:01:07,020 Okay, so let's start 30 00:01:07,020 --> 00:01:09,030 with the first function in the robot module, 31 00:01:09,030 --> 00:01:10,560 which is called reverse. 32 00:01:10,560 --> 00:01:13,560 It takes one input or argument, the direction, 33 00:01:13,560 --> 00:01:15,060 and from the doc string here, 34 00:01:15,060 --> 00:01:17,398 I see that it rotates the robot 180 degrees 35 00:01:17,398 --> 00:01:19,680 and returns its new direction. 36 00:01:19,680 --> 00:01:23,219 It also says the direction can either be left or right. 37 00:01:23,219 --> 00:01:25,500 So if I go back to main.py, 38 00:01:25,500 --> 00:01:26,580 I can call this function 39 00:01:26,580 --> 00:01:29,700 using the module name, robot.reverse. 40 00:01:29,700 --> 00:01:32,040 It's argument can be the string left or right. 41 00:01:32,040 --> 00:01:33,780 So let's say left for now, 42 00:01:33,780 --> 00:01:36,060 and then we know it returns the new direction. 43 00:01:36,060 --> 00:01:39,780 So I wanna store that value in a variable. 44 00:01:39,780 --> 00:01:42,000 Let's run it and see what it does. 45 00:01:42,000 --> 00:01:42,833 Nice. 46 00:01:42,833 --> 00:01:44,430 So this function is returning right, 47 00:01:44,430 --> 00:01:46,650 because it's reversing left. 48 00:01:46,650 --> 00:01:48,572 And then if I swap that and I try and reverse right, 49 00:01:48,572 --> 00:01:50,436 it returns left. 50 00:01:50,436 --> 00:01:52,740 Okay, not that interesting by itself. 51 00:01:52,740 --> 00:01:54,930 Let's see what this draw function does. 52 00:01:54,930 --> 00:01:57,090 It takes three inputs, the position, direction, 53 00:01:57,090 --> 00:01:59,750 and grid size, and it says it displays the robot's position 54 00:01:59,750 --> 00:02:01,749 and direction on the grid. 55 00:02:01,749 --> 00:02:04,140 Note that it doesn't say that it returned something, 56 00:02:04,140 --> 00:02:08,635 so it might just display and not return an output value. 57 00:02:08,635 --> 00:02:12,180 Back to main.py, let's call robot.draw. 58 00:02:12,180 --> 00:02:14,550 And we wanna give it a position, I don't know, 59 00:02:14,550 --> 00:02:16,980 let's say three, and then let's pass in the direction 60 00:02:16,980 --> 00:02:18,720 we got back from reverse. 61 00:02:18,720 --> 00:02:20,160 And it needs a grid size, 62 00:02:20,160 --> 00:02:22,680 which I guess needs to be bigger than the position. 63 00:02:22,680 --> 00:02:24,022 So let's say 10. 64 00:02:24,022 --> 00:02:25,560 Okay, nice. 65 00:02:25,560 --> 00:02:27,540 It looks like it's printing a little representation 66 00:02:27,540 --> 00:02:29,321 of the robot, and we can see that the arrow is pointing left 67 00:02:29,321 --> 00:02:31,912 because the robot is facing left. 68 00:02:31,912 --> 00:02:35,760 Let's draw the robot before we reversed it too. 69 00:02:35,760 --> 00:02:38,400 The position and grid size will be the same, 70 00:02:38,400 --> 00:02:39,660 but the direction will be right, 71 00:02:39,660 --> 00:02:42,090 so let's actually store that in the variable up here. 72 00:02:42,090 --> 00:02:44,490 And actually let's store the position and grid size 73 00:02:44,490 --> 00:02:45,390 in a variable as well, 74 00:02:45,390 --> 00:02:47,673 so we're not repeating three, three, 10, 10. 75 00:02:47,673 --> 00:02:51,930 Note that I'm not making any changes to the robot.py file. 76 00:02:51,930 --> 00:02:53,640 I'm just using this module. 77 00:02:53,640 --> 00:02:56,087 All of my code is going in main.py. 78 00:02:56,087 --> 00:02:58,920 Let's try one more function, move forward. 79 00:02:58,920 --> 00:03:01,860 We see that it takes a position, direction, and grid size, 80 00:03:01,860 --> 00:03:04,380 and it returns the robot's new position. 81 00:03:04,380 --> 00:03:07,440 So back to main.py, we call robot.moveforward 82 00:03:07,440 --> 00:03:08,970 and we pass in the robot position, 83 00:03:08,970 --> 00:03:10,770 the direction, and the size. 84 00:03:10,770 --> 00:03:12,360 And since it returns the new position, 85 00:03:12,360 --> 00:03:14,362 we're going to store the result of that function call 86 00:03:14,362 --> 00:03:16,890 in the robot position variable. 87 00:03:16,890 --> 00:03:17,884 And so we can see what happens. 88 00:03:17,884 --> 00:03:20,251 Let's call draw afterward. 89 00:03:20,251 --> 00:03:22,530 Now that we've experimented, 90 00:03:22,530 --> 00:03:24,750 we've got a good sense for how the robot module works. 91 00:03:24,750 --> 00:03:27,990 So let's try building this into a more interesting program. 92 00:03:27,990 --> 00:03:30,060 Maybe we want the robot to randomly decide 93 00:03:30,060 --> 00:03:32,820 whether it moves forward or reverses at any point. 94 00:03:32,820 --> 00:03:34,590 So I could generate a random number, 95 00:03:34,590 --> 00:03:37,560 and depending on the result, decide which function to call. 96 00:03:37,560 --> 00:03:39,660 That means I'm gonna need the random module. 97 00:03:39,660 --> 00:03:42,073 By convention, usually we alphabetize our imports, 98 00:03:42,073 --> 00:03:44,022 so I'm gonna put random first. 99 00:03:44,022 --> 00:03:45,949 And because there are two possible outcomes, 100 00:03:45,949 --> 00:03:49,110 I'll generate a random number between one and two. 101 00:03:49,110 --> 00:03:51,300 If I get a one, I'll have the robot move forward, 102 00:03:51,300 --> 00:03:53,637 and if I get a two, I'll have it reverse. 103 00:03:53,637 --> 00:03:56,970 Now I wanna generate three random numbers, 104 00:03:56,970 --> 00:03:59,550 because I want the robot to take three random actions. 105 00:03:59,550 --> 00:04:00,383 I don't want it 106 00:04:00,383 --> 00:04:03,210 to perform the same one random action three times. 107 00:04:03,210 --> 00:04:06,570 So I need to have three separate randint function calls. 108 00:04:06,570 --> 00:04:08,610 In fact, I'll just copy this whole block of code, 109 00:04:08,610 --> 00:04:11,149 because I want all of this functionality to repeat. 110 00:04:11,149 --> 00:04:13,500 Now, if I run the program a few times, 111 00:04:13,500 --> 00:04:15,749 I can see that the robot's moving randomly. 112 00:04:15,749 --> 00:04:16,860 One last thing. 113 00:04:16,860 --> 00:04:19,380 Maybe let's have the robot start at a random position 114 00:04:19,380 --> 00:04:20,640 each time too. 115 00:04:20,640 --> 00:04:22,290 The robot's gotta start on the grid. 116 00:04:22,290 --> 00:04:24,030 So the start should be one, 117 00:04:24,030 --> 00:04:27,150 and the stop should be the grid size. 118 00:04:27,150 --> 00:04:28,470 And now when I run the program, 119 00:04:28,470 --> 00:04:31,486 the robot's starting at a random position each time. 120 00:04:31,486 --> 00:04:33,750 Pretty cool how quickly we were able 121 00:04:33,750 --> 00:04:36,420 to get a complex program like that up and running. 122 00:04:36,420 --> 00:04:38,364 That's the power of using modules. 123 00:04:38,364 --> 00:04:40,950 We can build off a functionality other people 124 00:04:40,950 --> 00:04:42,898 have already written to expand the possibilities 125 00:04:42,898 --> 00:04:44,193 of our programs.