from byubit import Bit @Bit.run_from_empty(5, 3) def go(bit): while bit.front_clear(): bit.move() bit.paint('green')
Look at the
go_green.py file and learn about how we can define our own functions.
go, we use
bitto refer to Bit
go_greenwe could use
jousting_dogsto refer to whatever was passed to it
goand the scope of
bitpoints to something on the heap (a bit world)
cosmonow points to that same thing on the heap
go_greenis different than
cosmonot so much,
Try running the code:
Didn't invoke the function: missing parentheses
Missing positional argument. The name the error gives you is the name of the parameter in the function you tried to call.
How can we figure out where the problem was? For example, was it the first or second call to
In coming weeks, we'll talk about other tools that make this easier in complex programs.
For now, you can use
bit.snapshot to put breadcrumbs in the program to figure out where the problem is happening.
When naming a function:
_(next to the zero key + shift) to break up compound names
gois different from
from byubit import Bit def go_green(bit): """ Bit moves in the direction it is already facing and goes until it is blocked in front Bit paints each square green, except for the starting square """ while bit.front_clear(): bit.move() bit.paint('green') @Bit.run_from_empty(4, 3) def go(bit): go_green(bit) bit.left() go_green(bit)
"""allow us to include multiple lines in the same string.
The concept of creating and naming new ideas is called abstraction
Bit gives us
What new verbs would make this job even easier?
We've done problems like this before (nested while, etc.), but this is the first time we are using functions to define the pieces. The emphasis of this exercise is on the decomposition and abstraction of the parts, and then putting those parts together.
Several strategies to consider:
Which seems easiest? Why?
fill_row_with_blue(bit)(even though it isn't defined yet)
Then define and implement
What are the boundary conditions of
The event stream pattern works well: the event is running into a jumpable wall (left clear). The event loop runs until Bit cannot jump anymore.
if/elseas opposed to a single "jump hurdle" event
The mosaic pattern could work: move to the base of the first hurdle, then jump hurdles (which includes advancing to the next) until left is not clear.
Approach this using the event-stream pattern, instead of the mosaic pattern used by Stanford
Note that "up" and "over" are similar, vs "down"