[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.21,0:00:01.29,Default,,0000,0000,0000,,- [Instructor] Let's design a program Dialogue: 0,0:00:01.29,0:00:03.93,Default,,0000,0000,0000,,that imports functionality\Nfrom another file. Dialogue: 0,0:00:03.93,0:00:06.09,Default,,0000,0000,0000,,When programming teams\Ncollaborate on projects, Dialogue: 0,0:00:06.09,0:00:08.82,Default,,0000,0000,0000,,they're often writing code\Nacross multiple files. Dialogue: 0,0:00:08.82,0:00:10.19,Default,,0000,0000,0000,,They package their work into functions Dialogue: 0,0:00:10.19,0:00:13.05,Default,,0000,0000,0000,,and then share them for\Nother team members to use. Dialogue: 0,0:00:13.05,0:00:15.36,Default,,0000,0000,0000,,This is just another form of a module. Dialogue: 0,0:00:15.36,0:00:16.84,Default,,0000,0000,0000,,Here, my teammate wrote a module Dialogue: 0,0:00:16.84,0:00:19.47,Default,,0000,0000,0000,,for simulating a robot's movement. Dialogue: 0,0:00:19.47,0:00:21.90,Default,,0000,0000,0000,,We can open up the file and\Ntake a look at the code, Dialogue: 0,0:00:21.90,0:00:24.09,Default,,0000,0000,0000,,but we may not always have\Nthe background knowledge Dialogue: 0,0:00:24.09,0:00:27.51,Default,,0000,0000,0000,,or time to understand the\Ndetails of how it works. Dialogue: 0,0:00:27.51,0:00:29.52,Default,,0000,0000,0000,,The good news is, that doesn't matter. Dialogue: 0,0:00:29.52,0:00:30.65,Default,,0000,0000,0000,,The only thing I need to understand Dialogue: 0,0:00:30.65,0:00:33.75,Default,,0000,0000,0000,,is the documentation of how to use it. Dialogue: 0,0:00:33.75,0:00:34.98,Default,,0000,0000,0000,,If you notice up here, Dialogue: 0,0:00:34.98,0:00:38.43,Default,,0000,0000,0000,,we've been writing our code\Nin a file called main.py. Dialogue: 0,0:00:38.43,0:00:41.04,Default,,0000,0000,0000,,The .py is the file\Nextension for Python code, Dialogue: 0,0:00:41.04,0:00:42.90,Default,,0000,0000,0000,,just like jpeg is for images. Dialogue: 0,0:00:42.90,0:00:44.55,Default,,0000,0000,0000,,Then by convention in Python, Dialogue: 0,0:00:44.55,0:00:47.13,Default,,0000,0000,0000,,we name the file with\Nour main logic, main. Dialogue: 0,0:00:47.13,0:00:49.78,Default,,0000,0000,0000,,In the Khan Academy IDE,\Nwhen we press the run button, Dialogue: 0,0:00:49.78,0:00:52.47,Default,,0000,0000,0000,,it runs the code in main.py. Dialogue: 0,0:00:52.47,0:00:56.04,Default,,0000,0000,0000,,However, we can add other files\Nor modules to our program, Dialogue: 0,0:00:56.04,0:00:59.43,Default,,0000,0000,0000,,then import them into main.py\Nand use their functions there. Dialogue: 0,0:00:59.43,0:01:01.77,Default,,0000,0000,0000,,So if we go back to our main.py file, Dialogue: 0,0:01:01.77,0:01:02.96,Default,,0000,0000,0000,,we can import the robot module Dialogue: 0,0:01:02.96,0:01:05.59,Default,,0000,0000,0000,,and then call one of these functions. Dialogue: 0,0:01:05.59,0:01:07.02,Default,,0000,0000,0000,,Okay, so let's start Dialogue: 0,0:01:07.02,0:01:09.03,Default,,0000,0000,0000,,with the first function\Nin the robot module, Dialogue: 0,0:01:09.03,0:01:10.56,Default,,0000,0000,0000,,which is called reverse. Dialogue: 0,0:01:10.56,0:01:13.56,Default,,0000,0000,0000,,It takes one input or\Nargument, the direction, Dialogue: 0,0:01:13.56,0:01:15.06,Default,,0000,0000,0000,,and from the doc string here, Dialogue: 0,0:01:15.06,0:01:17.40,Default,,0000,0000,0000,,I see that it rotates\Nthe robot 180 degrees Dialogue: 0,0:01:17.40,0:01:19.68,Default,,0000,0000,0000,,and returns its new direction. Dialogue: 0,0:01:19.68,0:01:23.22,Default,,0000,0000,0000,,It also says the direction\Ncan either be left or right. Dialogue: 0,0:01:23.22,0:01:25.50,Default,,0000,0000,0000,,So if I go back to main.py, Dialogue: 0,0:01:25.50,0:01:26.58,Default,,0000,0000,0000,,I can call this function Dialogue: 0,0:01:26.58,0:01:29.70,Default,,0000,0000,0000,,using the module name, robot.reverse. Dialogue: 0,0:01:29.70,0:01:32.04,Default,,0000,0000,0000,,It's argument can be the\Nstring left or right. Dialogue: 0,0:01:32.04,0:01:33.78,Default,,0000,0000,0000,,So let's say left for now, Dialogue: 0,0:01:33.78,0:01:36.06,Default,,0000,0000,0000,,and then we know it\Nreturns the new direction. Dialogue: 0,0:01:36.06,0:01:39.78,Default,,0000,0000,0000,,So I wanna store that value in a variable. Dialogue: 0,0:01:39.78,0:01:42.00,Default,,0000,0000,0000,,Let's run it and see what it does. Dialogue: 0,0:01:42.00,0:01:42.83,Default,,0000,0000,0000,,Nice. Dialogue: 0,0:01:42.83,0:01:44.43,Default,,0000,0000,0000,,So this function is returning right, Dialogue: 0,0:01:44.43,0:01:46.65,Default,,0000,0000,0000,,because it's reversing left. Dialogue: 0,0:01:46.65,0:01:48.57,Default,,0000,0000,0000,,And then if I swap that and\NI try and reverse right, Dialogue: 0,0:01:48.57,0:01:50.44,Default,,0000,0000,0000,,it returns left. Dialogue: 0,0:01:50.44,0:01:52.74,Default,,0000,0000,0000,,Okay, not that interesting by itself. Dialogue: 0,0:01:52.74,0:01:54.93,Default,,0000,0000,0000,,Let's see what this draw function does. Dialogue: 0,0:01:54.93,0:01:57.09,Default,,0000,0000,0000,,It takes three inputs,\Nthe position, direction, Dialogue: 0,0:01:57.09,0:01:59.75,Default,,0000,0000,0000,,and grid size, and it says it\Ndisplays the robot's position Dialogue: 0,0:01:59.75,0:02:01.75,Default,,0000,0000,0000,,and direction on the grid. Dialogue: 0,0:02:01.75,0:02:04.14,Default,,0000,0000,0000,,Note that it doesn't say\Nthat it returned something, Dialogue: 0,0:02:04.14,0:02:08.64,Default,,0000,0000,0000,,so it might just display and\Nnot return an output value. Dialogue: 0,0:02:08.64,0:02:12.18,Default,,0000,0000,0000,,Back to main.py, let's call robot.draw. Dialogue: 0,0:02:12.18,0:02:14.55,Default,,0000,0000,0000,,And we wanna give it a\Nposition, I don't know, Dialogue: 0,0:02:14.55,0:02:16.98,Default,,0000,0000,0000,,let's say three, and then\Nlet's pass in the direction Dialogue: 0,0:02:16.98,0:02:18.72,Default,,0000,0000,0000,,we got back from reverse. Dialogue: 0,0:02:18.72,0:02:20.16,Default,,0000,0000,0000,,And it needs a grid size, Dialogue: 0,0:02:20.16,0:02:22.68,Default,,0000,0000,0000,,which I guess needs to be\Nbigger than the position. Dialogue: 0,0:02:22.68,0:02:24.02,Default,,0000,0000,0000,,So let's say 10. Dialogue: 0,0:02:24.02,0:02:25.56,Default,,0000,0000,0000,,Okay, nice. Dialogue: 0,0:02:25.56,0:02:27.54,Default,,0000,0000,0000,,It looks like it's printing\Na little representation Dialogue: 0,0:02:27.54,0:02:29.32,Default,,0000,0000,0000,,of the robot, and we can see\Nthat the arrow is pointing left Dialogue: 0,0:02:29.32,0:02:31.91,Default,,0000,0000,0000,,because the robot is facing left. Dialogue: 0,0:02:31.91,0:02:35.76,Default,,0000,0000,0000,,Let's draw the robot\Nbefore we reversed it too. Dialogue: 0,0:02:35.76,0:02:38.40,Default,,0000,0000,0000,,The position and grid\Nsize will be the same, Dialogue: 0,0:02:38.40,0:02:39.66,Default,,0000,0000,0000,,but the direction will be right, Dialogue: 0,0:02:39.66,0:02:42.09,Default,,0000,0000,0000,,so let's actually store that\Nin the variable up here. Dialogue: 0,0:02:42.09,0:02:44.49,Default,,0000,0000,0000,,And actually let's store\Nthe position and grid size Dialogue: 0,0:02:44.49,0:02:45.39,Default,,0000,0000,0000,,in a variable as well, Dialogue: 0,0:02:45.39,0:02:47.67,Default,,0000,0000,0000,,so we're not repeating\Nthree, three, 10, 10. Dialogue: 0,0:02:47.67,0:02:51.93,Default,,0000,0000,0000,,Note that I'm not making any\Nchanges to the robot.py file. Dialogue: 0,0:02:51.93,0:02:53.64,Default,,0000,0000,0000,,I'm just using this module. Dialogue: 0,0:02:53.64,0:02:56.09,Default,,0000,0000,0000,,All of my code is going in main.py. Dialogue: 0,0:02:56.09,0:02:58.92,Default,,0000,0000,0000,,Let's try one more function, move forward. Dialogue: 0,0:02:58.92,0:03:01.86,Default,,0000,0000,0000,,We see that it takes a position,\Ndirection, and grid size, Dialogue: 0,0:03:01.86,0:03:04.38,Default,,0000,0000,0000,,and it returns the robot's new position. Dialogue: 0,0:03:04.38,0:03:07.44,Default,,0000,0000,0000,,So back to main.py, we\Ncall robot.moveforward Dialogue: 0,0:03:07.44,0:03:08.97,Default,,0000,0000,0000,,and we pass in the robot position, Dialogue: 0,0:03:08.97,0:03:10.77,Default,,0000,0000,0000,,the direction, and the size. Dialogue: 0,0:03:10.77,0:03:12.36,Default,,0000,0000,0000,,And since it returns the new position, Dialogue: 0,0:03:12.36,0:03:14.36,Default,,0000,0000,0000,,we're going to store the\Nresult of that function call Dialogue: 0,0:03:14.36,0:03:16.89,Default,,0000,0000,0000,,in the robot position variable. Dialogue: 0,0:03:16.89,0:03:17.88,Default,,0000,0000,0000,,And so we can see what happens. Dialogue: 0,0:03:17.88,0:03:20.25,Default,,0000,0000,0000,,Let's call draw afterward. Dialogue: 0,0:03:20.25,0:03:22.53,Default,,0000,0000,0000,,Now that we've experimented, Dialogue: 0,0:03:22.53,0:03:24.75,Default,,0000,0000,0000,,we've got a good sense for\Nhow the robot module works. Dialogue: 0,0:03:24.75,0:03:27.99,Default,,0000,0000,0000,,So let's try building this into\Na more interesting program. Dialogue: 0,0:03:27.99,0:03:30.06,Default,,0000,0000,0000,,Maybe we want the robot to randomly decide Dialogue: 0,0:03:30.06,0:03:32.82,Default,,0000,0000,0000,,whether it moves forward\Nor reverses at any point. Dialogue: 0,0:03:32.82,0:03:34.59,Default,,0000,0000,0000,,So I could generate a random number, Dialogue: 0,0:03:34.59,0:03:37.56,Default,,0000,0000,0000,,and depending on the result,\Ndecide which function to call. Dialogue: 0,0:03:37.56,0:03:39.66,Default,,0000,0000,0000,,That means I'm gonna\Nneed the random module. Dialogue: 0,0:03:39.66,0:03:42.07,Default,,0000,0000,0000,,By convention, usually we\Nalphabetize our imports, Dialogue: 0,0:03:42.07,0:03:44.02,Default,,0000,0000,0000,,so I'm gonna put random first. Dialogue: 0,0:03:44.02,0:03:45.95,Default,,0000,0000,0000,,And because there are\Ntwo possible outcomes, Dialogue: 0,0:03:45.95,0:03:49.11,Default,,0000,0000,0000,,I'll generate a random\Nnumber between one and two. Dialogue: 0,0:03:49.11,0:03:51.30,Default,,0000,0000,0000,,If I get a one, I'll have\Nthe robot move forward, Dialogue: 0,0:03:51.30,0:03:53.64,Default,,0000,0000,0000,,and if I get a two, I'll have it reverse. Dialogue: 0,0:03:53.64,0:03:56.97,Default,,0000,0000,0000,,Now I wanna generate three random numbers, Dialogue: 0,0:03:56.97,0:03:59.55,Default,,0000,0000,0000,,because I want the robot to\Ntake three random actions. Dialogue: 0,0:03:59.55,0:04:00.38,Default,,0000,0000,0000,,I don't want it Dialogue: 0,0:04:00.38,0:04:03.21,Default,,0000,0000,0000,,to perform the same one\Nrandom action three times. Dialogue: 0,0:04:03.21,0:04:06.57,Default,,0000,0000,0000,,So I need to have three\Nseparate randint function calls. Dialogue: 0,0:04:06.57,0:04:08.61,Default,,0000,0000,0000,,In fact, I'll just copy\Nthis whole block of code, Dialogue: 0,0:04:08.61,0:04:11.15,Default,,0000,0000,0000,,because I want all of this\Nfunctionality to repeat. Dialogue: 0,0:04:11.15,0:04:13.50,Default,,0000,0000,0000,,Now, if I run the program a few times, Dialogue: 0,0:04:13.50,0:04:15.75,Default,,0000,0000,0000,,I can see that the\Nrobot's moving randomly. Dialogue: 0,0:04:15.75,0:04:16.86,Default,,0000,0000,0000,,One last thing. Dialogue: 0,0:04:16.86,0:04:19.38,Default,,0000,0000,0000,,Maybe let's have the robot\Nstart at a random position Dialogue: 0,0:04:19.38,0:04:20.64,Default,,0000,0000,0000,,each time too. Dialogue: 0,0:04:20.64,0:04:22.29,Default,,0000,0000,0000,,The robot's gotta start on the grid. Dialogue: 0,0:04:22.29,0:04:24.03,Default,,0000,0000,0000,,So the start should be one, Dialogue: 0,0:04:24.03,0:04:27.15,Default,,0000,0000,0000,,and the stop should be the grid size. Dialogue: 0,0:04:27.15,0:04:28.47,Default,,0000,0000,0000,,And now when I run the program, Dialogue: 0,0:04:28.47,0:04:31.49,Default,,0000,0000,0000,,the robot's starting at a\Nrandom position each time. Dialogue: 0,0:04:31.49,0:04:33.75,Default,,0000,0000,0000,,Pretty cool how quickly we were able Dialogue: 0,0:04:33.75,0:04:36.42,Default,,0000,0000,0000,,to get a complex program\Nlike that up and running. Dialogue: 0,0:04:36.42,0:04:38.36,Default,,0000,0000,0000,,That's the power of using modules. Dialogue: 0,0:04:38.36,0:04:40.95,Default,,0000,0000,0000,,We can build off a\Nfunctionality other people Dialogue: 0,0:04:40.95,0:04:42.90,Default,,0000,0000,0000,,have already written to\Nexpand the possibilities Dialogue: 0,0:04:42.90,0:04:44.19,Default,,0000,0000,0000,,of our programs.