< Return to Video

Modules program design: robot sim | Intro to computer science - Python | Khan Academy

  • 0:00 - 0:01
    - [Instructor] Let's design a program
  • 0:01 - 0:04
    that imports functionality
    from another file.
  • 0:04 - 0:06
    When programming teams
    collaborate on projects,
  • 0:06 - 0:09
    they're often writing code
    across multiple files.
  • 0:09 - 0:10
    They package their work into functions
  • 0:10 - 0:13
    and then share them for
    other team members to use.
  • 0:13 - 0:15
    This is just another form of a module.
  • 0:15 - 0:17
    Here, my teammate wrote a module
  • 0:17 - 0:19
    for simulating a robot's movement.
  • 0:19 - 0:22
    We can open up the file and
    take a look at the code,
  • 0:22 - 0:24
    but we may not always have
    the background knowledge
  • 0:24 - 0:28
    or time to understand the
    details of how it works.
  • 0:28 - 0:30
    The good news is, that doesn't matter.
  • 0:30 - 0:31
    The only thing I need to understand
  • 0:31 - 0:34
    is the documentation of how to use it.
  • 0:34 - 0:35
    If you notice up here,
  • 0:35 - 0:38
    we've been writing our code
    in a file called main.py.
  • 0:38 - 0:41
    The .py is the file
    extension for Python code,
  • 0:41 - 0:43
    just like jpeg is for images.
  • 0:43 - 0:45
    Then by convention in Python,
  • 0:45 - 0:47
    we name the file with
    our main logic, main.
  • 0:47 - 0:50
    In the Khan Academy IDE,
    when we press the run button,
  • 0:50 - 0:52
    it runs the code in main.py.
  • 0:52 - 0:56
    However, we can add other files
    or modules to our program,
  • 0:56 - 0:59
    then import them into main.py
    and use their functions there.
  • 0:59 - 1:02
    So if we go back to our main.py file,
  • 1:02 - 1:03
    we can import the robot module
  • 1:03 - 1:06
    and then call one of these functions.
  • 1:06 - 1:07
    Okay, so let's start
  • 1:07 - 1:09
    with the first function
    in the robot module,
  • 1:09 - 1:11
    which is called reverse.
  • 1:11 - 1:14
    It takes one input or
    argument, the direction,
  • 1:14 - 1:15
    and from the doc string here,
  • 1:15 - 1:17
    I see that it rotates
    the robot 180 degrees
  • 1:17 - 1:20
    and returns its new direction.
  • 1:20 - 1:23
    It also says the direction
    can either be left or right.
  • 1:23 - 1:26
    So if I go back to main.py,
  • 1:26 - 1:27
    I can call this function
  • 1:27 - 1:30
    using the module name, robot.reverse.
  • 1:30 - 1:32
    It's argument can be the
    string left or right.
  • 1:32 - 1:34
    So let's say left for now,
  • 1:34 - 1:36
    and then we know it
    returns the new direction.
  • 1:36 - 1:40
    So I wanna store that value in a variable.
  • 1:40 - 1:42
    Let's run it and see what it does.
  • 1:42 - 1:43
    Nice.
  • 1:43 - 1:44
    So this function is returning right,
  • 1:44 - 1:47
    because it's reversing left.
  • 1:47 - 1:49
    And then if I swap that and
    I try and reverse right,
  • 1:49 - 1:50
    it returns left.
  • 1:50 - 1:53
    Okay, not that interesting by itself.
  • 1:53 - 1:55
    Let's see what this draw function does.
  • 1:55 - 1:57
    It takes three inputs,
    the position, direction,
  • 1:57 - 2:00
    and grid size, and it says it
    displays the robot's position
  • 2:00 - 2:02
    and direction on the grid.
  • 2:02 - 2:04
    Note that it doesn't say
    that it returned something,
  • 2:04 - 2:09
    so it might just display and
    not return an output value.
  • 2:09 - 2:12
    Back to main.py, let's call robot.draw.
  • 2:12 - 2:15
    And we wanna give it a
    position, I don't know,
  • 2:15 - 2:17
    let's say three, and then
    let's pass in the direction
  • 2:17 - 2:19
    we got back from reverse.
  • 2:19 - 2:20
    And it needs a grid size,
  • 2:20 - 2:23
    which I guess needs to be
    bigger than the position.
  • 2:23 - 2:24
    So let's say 10.
  • 2:24 - 2:26
    Okay, nice.
  • 2:26 - 2:28
    It looks like it's printing
    a little representation
  • 2:28 - 2:29
    of the robot, and we can see
    that the arrow is pointing left
  • 2:29 - 2:32
    because the robot is facing left.
  • 2:32 - 2:36
    Let's draw the robot
    before we reversed it too.
  • 2:36 - 2:38
    The position and grid
    size will be the same,
  • 2:38 - 2:40
    but the direction will be right,
  • 2:40 - 2:42
    so let's actually store that
    in the variable up here.
  • 2:42 - 2:44
    And actually let's store
    the position and grid size
  • 2:44 - 2:45
    in a variable as well,
  • 2:45 - 2:48
    so we're not repeating
    three, three, 10, 10.
  • 2:48 - 2:52
    Note that I'm not making any
    changes to the robot.py file.
  • 2:52 - 2:54
    I'm just using this module.
  • 2:54 - 2:56
    All of my code is going in main.py.
  • 2:56 - 2:59
    Let's try one more function, move forward.
  • 2:59 - 3:02
    We see that it takes a position,
    direction, and grid size,
  • 3:02 - 3:04
    and it returns the robot's new position.
  • 3:04 - 3:07
    So back to main.py, we
    call robot.moveforward
  • 3:07 - 3:09
    and we pass in the robot position,
  • 3:09 - 3:11
    the direction, and the size.
  • 3:11 - 3:12
    And since it returns the new position,
  • 3:12 - 3:14
    we're going to store the
    result of that function call
  • 3:14 - 3:17
    in the robot position variable.
  • 3:17 - 3:18
    And so we can see what happens.
  • 3:18 - 3:20
    Let's call draw afterward.
  • 3:20 - 3:23
    Now that we've experimented,
  • 3:23 - 3:25
    we've got a good sense for
    how the robot module works.
  • 3:25 - 3:28
    So let's try building this into
    a more interesting program.
  • 3:28 - 3:30
    Maybe we want the robot to randomly decide
  • 3:30 - 3:33
    whether it moves forward
    or reverses at any point.
  • 3:33 - 3:35
    So I could generate a random number,
  • 3:35 - 3:38
    and depending on the result,
    decide which function to call.
  • 3:38 - 3:40
    That means I'm gonna
    need the random module.
  • 3:40 - 3:42
    By convention, usually we
    alphabetize our imports,
  • 3:42 - 3:44
    so I'm gonna put random first.
  • 3:44 - 3:46
    And because there are
    two possible outcomes,
  • 3:46 - 3:49
    I'll generate a random
    number between one and two.
  • 3:49 - 3:51
    If I get a one, I'll have
    the robot move forward,
  • 3:51 - 3:54
    and if I get a two, I'll have it reverse.
  • 3:54 - 3:57
    Now I wanna generate three random numbers,
  • 3:57 - 4:00
    because I want the robot to
    take three random actions.
  • 4:00 - 4:00
    I don't want it
  • 4:00 - 4:03
    to perform the same one
    random action three times.
  • 4:03 - 4:07
    So I need to have three
    separate randint function calls.
  • 4:07 - 4:09
    In fact, I'll just copy
    this whole block of code,
  • 4:09 - 4:11
    because I want all of this
    functionality to repeat.
  • 4:11 - 4:14
    Now, if I run the program a few times,
  • 4:14 - 4:16
    I can see that the
    robot's moving randomly.
  • 4:16 - 4:17
    One last thing.
  • 4:17 - 4:19
    Maybe let's have the robot
    start at a random position
  • 4:19 - 4:21
    each time too.
  • 4:21 - 4:22
    The robot's gotta start on the grid.
  • 4:22 - 4:24
    So the start should be one,
  • 4:24 - 4:27
    and the stop should be the grid size.
  • 4:27 - 4:28
    And now when I run the program,
  • 4:28 - 4:31
    the robot's starting at a
    random position each time.
  • 4:31 - 4:34
    Pretty cool how quickly we were able
  • 4:34 - 4:36
    to get a complex program
    like that up and running.
  • 4:36 - 4:38
    That's the power of using modules.
  • 4:38 - 4:41
    We can build off a
    functionality other people
  • 4:41 - 4:43
    have already written to
    expand the possibilities
  • 4:43 - 4:44
    of our programs.
Title:
Modules program design: robot sim | Intro to computer science - Python | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
04:44

English subtitles

Revisions