Even More Reeborg!

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()andturn_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
.pyfile for both steps. - You've saved
library.pywith 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
-
Work through this world, then save your program.
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 calledturn_around(). Let's put them in a library and make them available to all our future programs. -
Copy and paste the code above into the library tab.

-
Switch back to the Python Code tab.

-
Add this line to the top of your file. Place it before any calls to library functions.
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 theturn_right()function, we would write: -
Make Reeborg pick up the star from the lawn outside its house and set it down inside the house. Use the new
turn_right()andturn_around()functions from the library in your program.
Step 8 — Asking Questions
-
Work through this world, then save your program.
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 Trueif Reeborg is on a goal tile (flag, home, or green tile),Falseotherwise.front_is_clear()Returns Trueif there is no obstacle (wall, etc.) in front of Reeborg,Falseotherwise.right_is_clear()Returns Trueif there is no obstacle (wall, etc.) to the right of Reeborg,Falseotherwise.wall_in_front()Returns Trueif there is a wall in front of Reeborg,Falseotherwise.wall_on_right()Returns Trueif there is a wall on the right side of Reeborg,Falseotherwise.object_here()Tests whether an object is on Reeborg's tile. carries_object()Tests whether Reeborg is carrying an object. is_facing_north()Returns Trueif Reeborg is currently facing north,Falseotherwise.We can use an
ifto ask these questions and take actions based on whether the answer isTrueorFalse:Warning
Notice the colon (
:) at the end of theifline!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
- 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 aslibrary.py.
Turn It In
- 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.