BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 1a โ€” Introduction to Bit

If you get a specific error involving byubit or webcolors

Open the terminal window in Pycharm. Its icon (>_) is found along the left side of the pycharm window, towards the bottom. Copy, paste, and enter

conda run -n cs110 python -m pip install -U byu_pytest_utils byubit

If you get an error saying that cs110 is not a conda environment, run the second command, then the first command.

conda create -n cs110 matplotlib pytest

After creating the conda environment, you must tell Pycharm to use it in your project. See Setting a conda environment in PyCharm

Getting Started

5 minutes

TAs

Refresh students on how to navigate the website. Show how to get to the lab1a page. Show how labs are submitted in Canvas (Gradescope is only for homeworks).

Ask if there are any students who have not completed lab0 - you may direct them to the TA office if there are several.

Students

Download the zip file, shown above. Extract its contents and move the lab1a folder into your cs110 folder. Your folder organization should look like this:

organization of files for lab 1a

Notice that in the lab1a folder you have a directory called worlds. This directory contains worlds that Bit can operate in. When you see a line like this in a Python file:

@Bit.worlds('red-dot')

this tells Bit to use the world called red-dot.start.txt inside of the worlds folder. It uses red-dot.finish.txt to check whether you reached the correct finishing world.

Exercise 1

5 minutes

(a) Can you figure out what this code does?

On a piece of scratch paper, draw out what you think this code will do:

from byubit import Bit


@Bit.empty_world(5, 3)
def do_stuff(bit):
    bit.paint('blue')
    bit.move()
    bit.move()
    bit.paint('green')
    bit.turn_left()
    bit.move()
    bit.move()
    bit.paint('red')


if __name__ == '__main__':
    do_stuff(Bit.new_bit)

(b) Run the code

Inside of lab1a, create a file called exercise1.py. TAs: Show how to create a file several times.

Copy and paste the above code into this file.

Then click on the green triangle next to the main block and run the code:

run exercise 1 in PyCharm

See if your drawing was correct!

If you ran into errors, notify the TA and follow the instructions above.

Exercise 2

5 minutes

(a) Can you figure out what this code does?

On a piece of scratch paper, draw out what you think this code will do:

from byubit import Bit


@Bit.empty_world(3, 3)
def do_stuff(bit):
    bit.move()
    bit.paint('blue')
    bit.turn_left()
    bit.move()
    bit.paint('blue')
    bit.move()
    bit.turn_left()
    bit.move()
    bit.paint('blue')
    bit.turn_right()
    bit.turn_right()
    bit.move()
    bit.move()
    bit.paint('blue')


if __name__ == '__main__':
    do_stuff(Bit.new_bit)

(b) Run the code

Inside of lab1a, create a file called exercise2.py. Copy and paste the above code into this file.

Find the main block and run the code. See if your drawing was correct!

Exercise 3

5 minutes

(a) Can you figure out what this code does?

On a piece of scratch paper, draw out what you think this code will do:

from byubit import Bit


@Bit.empty_world(3, 3)
def do_stuff(bit):
    bit.snapshot('start')
    bit.paint('blue')
    bit.move()
    bit.move()
    bit.snapshot('moved_two')
    bit.move()
    bit.move()
    bit.turn_left()
    bit.paint('green')
    bit.move()
    bit.move()
    bit.paint('red')


if __name__ == '__main__':
    do_stuff(Bit.new_bit)

(b) Run the code

Inside of lab1a, create a file called exercise3.py. Copy and paste the above code into this file.

Find the main block and run the code. See if your drawing was correct!

Buttons

Notice how after running exercise 3, there are now two new buttons labeled Jump. They correspond to the lines of code that have bit.snaphot(). You can jump between snapshots with the jump buttons.

Hotkeys

In addition to clicking the buttons, you can step through your code using the hotkeys shown in the hotkeys document.

Exercise 4: Red dot

10 minutes

You are given some starting code inside of red_dot.py:

code for the red dot problem

Whenever you see a function that has one statement in it โ€” pass โ€” this is a function where you need to write code. The pass statement is a special keyword in Python that does literally nothing. It just exists as a placeholder for the code you need to write.

For this problem, Bit starts in an empty 3x3 world:

a 3x3 bit world where the middle square is red

Write code in the red_dot() function so that the middle square is red. This is what the world should look like when Bit is finished:

a 3x3 bit world where the middle square is red

  1. To get started, delete pass and write:
bit.move()

Be sure the code is indented because in Python indentation matters:

red dot code

Run this code and you will see that you havenโ€™t solved the problem:

problem with the red dot code

Explore the bit window.

  • What do you think the exclamation point on the red background means?
  • Where is bit currently?
  • Where should bit end up?

Now write code to solve this problem.

After writing some code, explore the buttons.

  • What does First do?
  • How would the Next button be more useful than Last?
  • What do the words at the top of the window mean?
  • How do they change as you use the buttons?
  • How could you use this information to fix problems in your code?

When you get it right, the screen will tell you compare correct! at the top:

correct solution for the red dot problem

Exercise 5: All the colors

10 minutes

You are given some code in all-the-colors.py. Bit starts from a blank 5x6 world:

a blank 5x6 Bit world

Bit should finish with a world that looks like this:

a stripe of colors in the 2nd row

Run the code in all-the-colors.py. It will finish with a comparison error:

a comparison error for this problem

  • The top two exclamation points have a clear background, showing that these squares should be clear.
  • The right two exclamation points have a background that is blue or red, showing that these squares should be blue or red.
  • The empty Bit on the right edge shows the location and direction where Bit should be at the end of the problem.

Fix this code so that it works correctly. You should be able to run the code and see that you have it correct:

this problem with no comparison error

Grading

To finish this lab and receive a grade, take the canvas quiz.

No code is submitted for labs.

To submit homeworks, you will submit code to Gradescope. Find how to get to Gradescope from Canvas. The important files are any .py files that you edit; .txt files may be submitted, but will not be used.

After submitting a homework, but before marking it as completed, take the assignment completion survey.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. ๐Ÿ˜Š