Skip to content

Even More Reeborg!

Reeborg, a friendly robot, waving hello. Reeborg, a friendly robot, waving hello. Again.

Overview

You'll learn to share functions between programs with a library, and to use if statements with Reeborg's built-in sensing functions so your program can make decisions on its own.

Builds on

More Reeborg!. Keep your saved programs from that lab.

Before you start

Each mission below has its own button that loads the right world. Save your work after every world — you'll turn all of them in at the end. The world names (such as Step 4) are Reeborg's labels; the numbered instructions below are this lab's action steps.

You've got it when…

  • Your library holds turn_right() and turn_around(), and your Step 6 program imports and uses them to put the star inside the house.
  • Your Step 8 program finds every flower on its own — no matter where they are — puts them on the grey flowers, and returns to the flag.
  • You've saved a .py file for both steps.
  • You've saved library.py with your two programs.

Collaboration & AI

Work: On your own. You can compare notes with a neighbor, but write your own commands.

AI — AIAS Level 1, No AI: Write and debug your own code without AI assistance. What the levels mean.


Step 6 — Build a Library

  1. Work through this world, then save your program.

    Open Step 6

    We made a handy new turn_right() function in Step 4. You might have even copied and pasted it to Step 5.

    We'll probably want to be able to turn right in a lot of programs we write. When we want to share code with multiple programs, we can add it to a library.

    Below are two functions, turn_right() from before and a new function called turn_around(). Let's put them in a library and make them available to all our future programs.

    def turn_right():
        turn_left()
        turn_left()
        turn_left()
    
    def turn_around():
        turn_left()
        turn_left()
    
  2. Copy and paste the code above into the library tab.

    The tab bar with the library tab selected.

  3. Switch back to the Python Code tab.

    The tab bar with the Python Code tab selected.

  4. Add this line to the top of your file. Place it before any calls to library functions.

    import library
    

    When we want to use these functions in a new program, we must first import the library. This tells Python which library we want to use. Then we can use the library functions in our program, but we must add the word library. before the name of each function we want to use. For example, to use the turn_right() function, we would write:

    library.turn_right()
    
  5. Make Reeborg pick up the star from the lawn outside its house and set it down inside the house. Use the new turn_right() and turn_around() functions from the library in your program.

Step 8 — Asking Questions

  1. Work through this world, then save your program.

    Open Step 8

    Reeborg can call some functions to ask questions about its environment. Each is a "yes" or "no" question — each result can be used as a true/false condition. Use these functions as conditions to decide whether to act.

    Function What it answers
    at_goal() Returns True if Reeborg is on a goal tile (flag, home, or green tile), False otherwise.
    front_is_clear() Returns True if there is no obstacle (wall, etc.) in front of Reeborg, False otherwise.
    right_is_clear() Returns True if there is no obstacle (wall, etc.) to the right of Reeborg, False otherwise.
    wall_in_front() Returns True if there is a wall in front of Reeborg, False otherwise.
    wall_on_right() Returns True if there is a wall on the right side of Reeborg, False otherwise.
    object_here() Tests whether an object is on Reeborg's tile.
    carries_object() Tests whether Reeborg is carrying an object.
    is_facing_north() Returns True if Reeborg is currently facing north, False otherwise.

    We can use an if to ask these questions and take actions based on whether the answer is True or False:

    if front_is_clear():
        move()
    

    Warning

    Notice the colon (:) at the end of the if line!

    Your mission: the flowers in this world change positions every time it is played, so we can't know ahead of time where the flowers will be.

    • Use the functions above to ask questions and make Reeborg find and collect all the flowers as it walks along the yard from left to right. After each move, if there is an object there, pick it up.
    • Put all the flowers down on grid space (2, 3), which is the grey flowers.
    • Return to the checkered flag at grid space (1, 3).

Check Your Work

  1. Reset and run Step 6, then run Step 8 several times to test different flower positions. Save Step 6.py, Step 8.py, and the code from the library tab as library.py.

Turn It In

  1. Submit these Python source files:
  • Step 6.py
  • Step 8.py
  • library.py

Include your library

Save the contents of the library tab separately from Python Code. When reopening your work in Reeborg, restore that code to the library tab before running a program that imports it.


How It's Graded

One score, from 0–4, covers your submitted Python source files.

Score Evidence
4 Excellent Both programs and library.py are submitted. Step 6 imports and uses both turning functions; Step 8 uses sensing and if to collect and deposit the flowers and return to the flag.
3 Above Average All required programs work and use the required constructs, with minor errors or omissions in code organization.
2 Average Some required work functions, but a mission, required construct, or source file is missing.
1 Below Average Programs are largely incomplete or nonfunctional.
0 Failing No submission or no evidence of the required work.

Credit: Adapted from Dan Schellenberg's Computer Science 20 textbook.