1.4. Practice Makes Perfect

The only way to become comfortable coding is to practice.

In the next meeting, we’ll start teaching you the rules of coding in Python. But for now, you can practice running and experimenting with programs that others have written.

We’ve included some programs for you to run and experiment with below.

1.4.1. Checker Board

Scroll the window contents and resize the editor so you can see the Run button and also a good 4 inches or so below the editor. Then run the program.

The instructions in lines 7–9 are assignment instructions. Executing these three assignments creates three variables, unit, rnum, and cnum, and assigns them the values 20, 8, and 8, respectively.

We’ll learn more about variables and values in our next meeting. Today, we just want to develop some intuition about how programmers them.

Run some experiments in which you change the values assigned to these variables. For example, try changing the value (number) 20 in line 7 to, say, 10 and rerun the program to see how changing the value assigned to unit affects what the program draws. Also run some experiments in which you change the values (numbers) in the other assignment instructions.

The instructions in lines 11 and 12 are also assignments. Executing line 11 creates two variables, last_col_color and last_row_color, and assigns them both the string value "black". Similarly, executing line 12 creates variables next_col_color and next_row_color and assigns them both the string value "white".

Run some experiments in which you change the color strings in lines 11 and 12. (They need to be legal Turtle Graphics color strings, for example, "red", "green", "blue", "yellow". In Python, strings are characters that are surrounded by quotes.)

What values would you assign to the variables in lines 7—12 of this program to get it to draw the following board (the small squares should be 40 pixels on each side)?

Picture of a 3x4 board of alternating green and red squares (40 pixels a side)

Try one last experiment with the Checker Board program: Add a hash symbol (#) to the front of the turtle.hideturtle() instruction (the last line of the program). Then run the program again.

Q-10: The hash symbol in Python has a special meaning: It means that the rest of the line is a comment and not a Python instruction. When you run a program, comments are ignored—a programmer adds comments to their code to help someone reading their code understand it.

So the # at the front of the turtle.hideturtle() instruction makes it a comment, which means that the instruction is ignored (not executed).

Based on your experiment and this explanation of comments, what do you think executing a turtle.hideturtle() instruction does?

1.4.2. Spirograph

Run the program.

Experiment with changing the values assigned to the variables in lines 9–11 and notice how their values affect what the program draws.

Challenge: What values would you assign to these variables to get the program to draw the following picture?

Picture created by a "Spirograph" program containing 24 circles and alternating red, then magenta, then blue

1.4.3. Turtle Star

Run the program.

This program is equivalent to the example program in the Turtle Graphics docs. The Runestone active code windows provide just one platform for running Python programs. Many other platforms can be used to run Python code. When this program is run on one popular platform (Idle), it produces the drawing shown below.

Picture produced by running the Turtle Star program in idle

Compare this picture to the one that is produced when you run the program in the active code window. What differences do you notice?

Q-16: Can you think of reasons why the same program might produce different results when executed on different platforms? clipart of a clock with wings

You have attempted of activities on this page