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. Make sure you understand what the starter code is doing.

Now change the drawing portion of the starter 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 on replit.

Note that picture.run() guarantees that the picture will continue to display even after the program is done executing. You can either end the program by clicking the “X” in the top-right corner of the canvas, or use ctrl-C to exit the program.

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 on replit:

  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 Files tab of replit. 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.)

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 on replit.

  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 Files tab replit, 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 in replit, 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 in replit, 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.