BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 2a - If

Preparation

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

1 minute

Download the zip file for this lab, located above. This zip file has worlds that you will use for Bit. Extract the files and put them in your cs110 directory in a folder called lab2a.

Exercise 1

5 minutes

Bit starts in this world:

a world with some red and green squares

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

On a piece of scratch paper, draw out what you think the final world will look like.

from byubit import Bit


def turn_or_change(bit):
    if bit.is_on_red():
        bit.turn_left()
    elif bit.is_on_green():
        bit.paint('blue')
    elif bit.is_on_blue():
        bit.paint('green')


@Bit.worlds('exercise1')
def go(bit):
    while bit.can_move_front():
        bit.move()
        turn_or_change(bit)


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

(b) Run the code

Inside of lab2a, run the file called exercise1.py and check your answer.

Exercise 2

5 minutes

Bit starts in this world:

a world with some red and green squares

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

On a piece of scratch paper, draw out where you think Bit will end up. Show which square and which direction Bit will face.

from byubit import Bit


def follow_rules(bit):
    if bit.is_on_blue():
        bit.turn_left()
    elif bit.is_on_green():
        bit.turn_right()
    elif bit.is_on_red():
        bit.turn_right()
        bit.turn_right()


@Bit.worlds('exercise2')
def go(bit):
    while bit.can_move_front():
        bit.move()
        follow_rules(bit)


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

(b) Run the code

Inside of lab2a, run the file called exercise2.py and check your answer.

Fix reds

15 minutes

For this problem, Bit starts in this world:

a world with some red squares and a green square

Bit needs to move to the green square, and then turn all the red squares to blue:

the red squares after the green square are turned to blue

(1) Draw the problem out with a friend. Use a drawing or a flow chart.

(2) Write the code with a friend. Use decomposition — break the problem up into multiple functions, each one doing a small thing.

(3) Come together as a class and discuss the solution.

Flowers

15 minutes

For this problem, Bit wants to grow some flowers. Bit starts in this world:

a world with some green squares

Each of the green squares represents a flower stem. Move Bit to each flower stem and grow a flower on top of it:

each green square has a red square above it

(1) Draw the problem out with a friend. Use a drawing or a flow chart.

(2) Write the code with a friend. Use decomposition — break the problem up into multiple functions, each one doing a small thing.

(3) Come together as a class and discuss the solution.

Wrapup

5 minutes

Discuss any questions about if statements

Grading

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

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