Warmup

Part A: Square Up

In this exercise, you’ll get a little experience drawing pictures in Python.

Open `square.py` in the Files tab. Note that there is already some code there. `import picture` allows us to use commands from `picture.py` to draw. (Note that you never need to open `picture.py`, and in fact probably shouldn’t, unless you want to be confused.) We can use functions from `picture.py` by typing `picture.`, followed by the name of the function we want to call. You can see this done in `square.py` with the starter code. Notice the comments which describe what each line does.

The pen works in the following way: at all times, it has a position and a direction. The two important commands to know are `picture.draw_forward()` and `picture.rotate()`. Calling `picture.draw_forward(x)` will move the pen’s position `x` pixels in whatever direction it is facing leaving a line between the start and finish. Calling `picture.rotate(t)` will rotate the pen `t` degrees clockwise (e.g., `picture.rotate(90)` is a right-angle turn).

To show you how the pen works, the starter code has a few lines which will move the pen around, and rotate it. Read through the lines and try to guess what the picture will look like. Then try running the program in the terminal. You should see a new file called `square_picture.png` show up in your list of files. Click on its name to see the picture and check whether your guess was right.

Make sure you understand what the starter code is doing before moving on! After each step below, it’s a good idea to run the program again and make sure you’re getting the output you expect.

Now our goal will be two-fold: (1) Change the code to draw a square and (2) change the code to draw a square using a `for` loop.

For Step 1, let’s remove the starter code and replace it with the code to draw a square. You should need to use `picture.rotate()` three times, and `picture.draw_forward()` four times. With a partner, write about what the code is doing in your own words in the `WARMUP.md` file.

Once you’ve finished Step 1, you might notice that some of the code you wrote looks the same as some of the other code. Specifically, each side of the square is drawn using one call to `picture.rotate()` and one call to `picture.draw_forward()`. Edit your code to use a `for` loop to draw the square. Your new code should have one `for` loop, one call to `picture.rotate()` and one call to `picture.draw_forward()`. Describe how you “loopified” your code in `WARMUP.md`.

Reminder: commit and push your code before you move on!

Part B: Pattern Practice

In Part 2 of this week’s lab, you will be using `for` loops to print interesting patterns of numbers and “*” signs. This exercise will give you some practice breaking these patterns down.

Here, we’ll consider patterns that look like this:

``````1 2 3
1 2 3
1 2 3

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
``````

With a partner, complete the following tasks and write your answers in the `warmup.md` file:

1. Briefly describe in words what’s going on with these patterns. If there are N rows, what is in each row?
2. Open `patternA.py` in the left panel’s “Explorer” section. The program asks the user for a number `N`. Add code to the `for` loop which prints the numbers 1 through `N`, separated by spaces.
3. Now add code to what you did in prompt 2 so that you print the line of numbers `N` times. (You can do this with two nested loops or two loops in sequence. For extra practice, try to figure out how to do it both ways.)

Reminder: commit and push your code before you move on!

Part C: Countdown!

This exercise is going to give you some practice with debugging, the most sacred of programming activities. Our goal will be to write a program that does the following:

1. Asks the user for a number `N` of minutes.
2. Prints “`X` minutes are left! `N-X` minutes have passed!” for each value of `X` from `N` to 0.

For example, with `N = 4`, the program will print:

``````4 minutes are left!
0 minutes have passed!

3 minutes are left!
1 minutes have passed!

2 minutes are left!
2 minutes have passed!

1 minutes are left!
3 minutes have passed!

0 minutes are left!
4 minutes have passed!
``````

Note that there are commands to delay execution of code, which could make this printing happen in real time, but we won’t do that here for simplicity’s sake.

With a partner, work through the prompts below. Write your answers in the `warmup.md` file.

1. Discuss with your partner how you would use a loop to implement this program. What variables would you use? How would they change as your loop runs?
2. One way you could implement the program is to maintain two variables, `time_remaining` and `time_passed` through the loop. `time_remaining` will track the number of minutes left, and `time_passed` will track the minutes that have elapsed. You use either one as the loop variable. Based on this description, write down how you would expect `time_remaining` and `time_passed` to change for the `N = 4` case above.

In the next three parts, you will debug three incorrect implementations of the countdown program.

1. Open `countdown1.py` in the file explorer and run it. The output is incorrect. Notice that the `range()` function seems to be counting down. Suggest a way of fixing the code without changing the inputs to `range()`. Try your suggestion to see if it works.
2. Open `countdown2.py` and run it. The output is incorrect. Examine the order of execution. In a few sentences, try to explain the problem and suggest a fix to the code. Try your suggestion to see if it works.
3. Open `countdown3.py` and run it. Although the program runs, the output is incorrect. What’s wrong in the construction of the `for` loop? In a few sentences, try to explain the problem and suggest a fix to the code. Try your suggestion to see if it works.

Reminder: commit and push your code before you move on!