Introduction to Bit¶

Learning Outcomes¶

  • pip install byubit
  • Introduction to Bit
    • move, left, right, paint
  • Overview of a Bit script
    • from byubit import Bit
    • @Bit.run
  • Syntax
    • indentation
    • quotes on strings
    • () on function calls
    • Casing
  • Running Bit scripts
    • Example in PyCharm
    • Demonstrate how to step through the code with the Bit UI
    • Demonstrate what it looks like when something is incorrect

Widely used packages are available with conda.

Less-widely used packages are available with pip.

We'll use pip to install byubit:

conda activate cs110
pip install byubit

Once we've installed a package, we can use it in a script.

Bit¶

Bit lives in a grid world, like a chessboard.

He can do four actions: move, left, right, and paint.

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def move_around(bit):
    bit.move()
    bit.move()
    bit.paint("red")
    
    bit.left()
    bit.move()
    bit.paint("green")
    
    bit.right()
    bit.move()
    bit.paint("blue")
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 robot or Sally or cosmo.

e.g.

def move_around(cosmo):
    cosmo.move()
    ...

We'll talk more about functions in a few lectures.

@Bit.run_from_empty(5,3)

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.

bit.move()
    bit.paint("red")

These lines are the instructions we have for bit.

bit.move is a function. Including () at the end means we want to call that function.

Calling 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.

Calling bit.paint("red") will paint the current space the specified color.

Valid colors are "red", "blue", and "green". (Notice the quotes ")

Indentation Matters!

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.

What picture will the following code produce?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def move_around(bit):
    bit.move()
    bit.left()
    bit.move()
    bit.paint("green")
    
    bit.right()
    bit.move()
    bit.paint("red")
    
    bit.right()
    bit.move()
    bit.paint("blue")

What code would you write to produce the following picture?

Sketch out your strategy on a piece of paper.

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def move_around(bit):
    bit.move()
    bit.move()
    bit.move()
    bit.left()
    bit.move()
    bit.paint("green")
    

What picture does the following code create?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def move_around(bit):
    bit.move
    bit.move
    bit.paint("green")

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 bit.move?

And the computer says

Yep.

When you include the (), it's like saying:

Hey computer, run the function named bit.move

What picture does the following code create?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def move_alot(bit):
    bit.move()
    bit.move()
    bit.move()
    bit.move()
    bit.move()
    bit.paint('green')

When you try to move the bit to an invalid space, you get an error.

What picture does the following code create?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def paint_stuff(bit):
    bit.move()
    bit.paint()

bit.paint requires one argument: the 'color'.

Remember the valid values are:

  • "red"
  • "blue"
  • "green"

You can also use single quotes:

  • 'red'
  • 'blue'
  • 'green'

What picture does the following code create?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def paint_stuff(bit):
    bit.move()
    bit.paint(red)

Remember the quotes!

What picture does the following code produce?

In [ ]:
from byubit import Bit

@Bit.run_from_empty(5,3)
def go(bit):
    bit.paint('green')    
    bit.Move()
    bit.paint('blue')

The casing (i.e. "upper-case" or "lower-case") matters!

Paint is not the same as paint

You can also run Bit in predefined worlds.

For example, '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:

In [ ]:
from byubit import Bit

@Bit.run("grassy_field")
def make_sky(bit):
    bit.paint('blue')
    
    

You can see that our function didn't quite deliver on the expected result.

What code could we use to finish the picture?

In [ ]:
@Bit.run("grassy_field")
def make_sky(bit):
    bit.paint("blue")
    

Finally, when you @Bit.run in a Python script, you can step through each step, one at a time.

bit_demo.py¶

In [ ]:
%%file bit_demo.py
from byubit import Bit

@Bit.run("grassy_field")
def make_some_sky(bit):
    bit.paint("blue")
    bit.move()
    bit.paint("blue")
    bit.move()
    bit.paint("blue")
    bit.move()
    bit.paint("blue")
    bit.move()
    bit.paint("blue")
    bit.move()
    bit.paint("blue")
In [ ]:
! python bit_demo.py

Key Ideas¶

  • from byubit import Bit let's us use Bit in our script
  • bit.move, bit.left, bit.right, and bit.paint are functions that make bit do something
    • But remember the ()!
    • bit.paint needs a color: 'red', 'blue', 'green', e.g. bit.paint('red')
  • @Bit.run takes a string that indicates a predefined Bit world to use
  • You can step through the action one step at a time