We can install code that other people wrote (called a "package") using the
pip command in our terminal.
pip to install
conda run -n cs110 pip install byubit
Once we've installed a package, we can use it in a script.
from byubit import Bit
This means that
byubit (what we
pip installed) has a thing called
Bit, and we want to use it.
def move_around(bit): ...
Here we define a function that will tell Bit what to do.
In this case, we named the function parameter
bit, but we could have named it
def move_around(cosmo): cosmo.move() ...
We'll talk more about functions in a few lectures.
This means that we want
Bit (the thing we imported from
byubit) to run our function.
In this case,
Bit will create an empty world that is 5 spaces wide and 3 spaces tall.
When Bit starts in an empty world, it always starts in the bottom-left corner facing right.
These lines are the instructions we have for
bit.move is a function. Including
() at the end means we want to call that function.
bit.move() makes the bit move one space in the direction it is facing.
bit.paint is a function. Including
("red") at the end means we want to call that function with
"red" as a parameter.
bit.paint("red") will paint the current space the specified color.
Valid colors are
"green". (Notice the quotes
This line is where we tell python we want to run the
move_around function we defined.
def line is where we defined the recipe, and the
move_around(Bit.new_bit) line is where we tell it to actually bake the cake.
Bit.new_bit is the bit that will explore the empty world we requested in
def move_around(bit): bit.move() bit.move() bit.paint("red") ####
You'll notice that the lines inside the function are indented 4 spaces. This matters.
Be sure to indent the code that belongs to a function. Later, we'll talk more about this, but this is enough to get you started.
if __name__ == '__main__':
We'll learn more about what this means later. For now, include it before the line where you call your primary function.
@Bit.empty_world(5, 3) def move_around(bit): # <-- Call this function... ... if __name__ == '__main__': move_around(Bit.new_bit) # ...down here!
What code would you write to produce the following picture?
Sketch out your strategy on a piece of paper.
Was this what you expected?
When you type the name of a fuction, but don't include the
() at the end, it's like saying:
Hey computer, did you know there is a function named
And the computer says
When you include the
(), it's like saying:
Hey computer, run the function named
When you try to move the bit to an invalid space, you get an error.
bit.paint requires one argument: the 'color'.
Remember the valid values are:
You can also use single quotes:
Remember the quotes!
The casing (i.e. "upper-case" or "lower-case") matters!
Paint is not the same as
You can also run Bit in predefined worlds.
'grassy_field' starts Bit in a world like this:
And it expects that by the time your function finishes, the world will look like this:
You can see that our function didn't quite deliver on the expected result.
What code could we use to finish the picture?
from byubit import Bitlet's us use
Bitin our script
bit.paintare functions that make bit do something
bit.paintneeds a color:
@Bit.worldstakes a string that indicates a predefined Bit world to use