Chapter 14 Exercises¶
Fix 5 errors in the code below so that it runs correctly. It will draw red and black horizontal stripes.
Add
*
at the end of line 1. Change line 4 toScreen
. Add a)
at the end of line 9. Indent line 15. Change line 18 togoto
.The code below draws three peaks, vertically. Change 1 thing to make it draw three peaks horizontally. (Hint: You have to change something that’s in the body of the for loop)
On line 7, change
<=
to>=
.Indent lines in the code below so that it runs correctly. It will stamp 4 turtles in two different colors at the corners of a square.
Indent lines 15 to 22 as shown below.
Fix the errors so the turtle stays in a straight vertical line without leaving the screen.
Fix the indentation first like below. Outside the for loop, turn the turtle 90 degrees. In the if statement, the turtle should go forward after turning. In the elif statement, the y position minus 100 should be less than the negative
maxY
. Make sure the else has a colon.Fix 5 errors in the code below so that it runs correctly. It will draw a repeating pattern from left to right until it hits the width of the window and then will move back to the left side of the window to continue the pattern.
Add
import
on line 23. Change line 26 tospace
. Change line 18 topendown
. Add a(
before the100
on line 3. Add a)
on line 6.The code currently draws 5 horizontal lines of alternating colors. Change it so that it draws 5 vertical lines of alternating colors.
Change all the width to height, and flip the two parameters in the
goto
method. For ease of reading, you can switch all instances ofx
andy
. Turn the turtle 90 degrees left before drawing.Change the code below to use
if
andelse
. Also fix any errors. You will need to change 3 lines. The code will draw random connected lines in alternating colors of red and black.Add a
:
at the end of line 12. Change line 14 toelse:
. Indent line 15.Fix the errors in the code so it alternates between printing a horizontal yellow line and a vertical black line.
Fix the indentation. In the if statement for even, it should be pendown() and it should go forward the height. In the odd if statement, it should turn right.
Fix the indention so that the code runs correctly. Two turtles will move towards each other and then turn around and move away from each other.
Indent lines 11-15 as shown below.
Change and fix the code below so that it draws random, but connected black and red lines (it should look like scribbling) only in the bottom right half of the drawing window.
Fix the indentation and add colons after the if statements. Change the range of
randX
to be(0, maxX)
and the range ofrandY
to be(minY, 0)
.The following code stamps a circle of turtles. Change the following code to use a different color per stamp and use at least 3 colors. You can use a counter and reset the counter to 0 after it reaches the number of colors (i.e. use a for loop and change color based off divisibility of each number). Use
if
,elif
, andelse
.Add lines 7-12 as shown below.
Add to the code so that
num
is a random number between 1 and 3 (inclusive), and change the if clauses to be if, elif, and else. The code should draw random lines with 3 different colors based off the value ofnum
.The following code stamps turtles in a spiral. Change the code below to cycle through at least 3 colors. Use
if
,elif
, andelse
.Add lines 7-12 as shown below.
The code currently makes the two turtles just draw a circle. Fix the errors on line 13 so that the turtles move towards each other and then turn around and move away from each other.
The if statement should be
mia.xcor() - jaz.xcor() < 40
.The following code draws vertical stripes alternating between red and black. Change and add code below to use 5 different colors. Use
y % 5
instead ofy % 2
to get 5 possible values.Modify lines 15 and 17 as shown below. Add lines 18 to 23.
Complete and add to the
turtleLoop
procedure so that when the turtles collide, they move away, then turn so that they move in the same direction. It should look like a mirror image divided across the vertical axis.Complete like shown below.
Write a function takes a number and returns a color. It will return ‘yellow’ if the number modulus 3 is 0, ‘blue’ if it is 1, and ‘green’ if it is 2.
Define the function as shown below and be sure to create tests that try all possible execution paths (all return values).
Write a procedure that takes in any number as the first parameter and a turtle as another parameter. The procedure should determine if the number is even or odd. If it is even have the turtle go right. If it is odd, have it go left. The procedure should get a random value between 1 and 2 (inclusive) and assign a color based on that number.
Define the procedure as shown below. Don’t forget to import the necessary modules.
Write code that draws a pattern with the turtle with at least 3 different colors used. The code must have a
for
loop and must have aif
statement inside the for loop that changes the color.Below is one possible example. Look for a for loop with an if statment inside of it that changes the color.
Write code that uses 2 turtles and a for loop to get a range of numbers. You should change the color of the based off if the number from the for loop is even or odd. The two turtles should move towards each other but turn away and move when they are about to intersect.
Here’s an example of one possibility.