322,572 views
39 votes
39 votes
On page 104, of Think Like a Computer Scientist, you learned about paired data. With paired data, you can use a for loop with two index variables to iterative through the data. In this exercise you will create a turtle drawing using pairs of data, where the first item of the pair is the distance to move forward , and the second item is the angle to turn.. Set up a list of pairs so that the turtle draws a house with a cross through the center, as show here. This should be done without going over any of the lines / edges more than once, and without lifting your pen.

Your first pair of data will be (100,135). In a list this will look like ls = [ (100, 135) ] Then you need to add the other pairs for the remaining lines and angles. The 100 represents the distance forward the turtle will travel. The 135 represents the angle the turtle will turn left.

Screen capture of run of program showing house figure

Hint: Your first line should be the bottom line. Then turn left 135 degrees and go forward. You may wish to create the drawing without the list first, then substitute a list and a for loop for the distances and angles.



Use two functions: a main() function that sets up screen and turtle objects, and a drawhouse() function that takes a turtle object as an argument.



Includes comments at the top to identify file name, project and a brief description.

For further documentation, include comment for each section of code. Write in Python asap

On page 104, of Think Like a Computer Scientist, you learned about paired data. With-example-1
User Anton Dovzhenko
by
3.1k points

1 Answer

17 votes
17 votes

Answer:

import turtle

def drawhouse(t):

# find the length of diagonal of a square with side = 100

# formula: diagonal = sqrt(2) * side

diagonal = 100 * (2 ** 0.5)

data = [(100, 135), (diagonal, -135), (100, -135), (diagonal, -135), (100, -45), (diagonal / 2, -90),

(diagonal / 2, -45), (100, 0)]

for dist, angle in data:

t.forward(dist)

t.left(angle)

def main():

t = turtle.Turtle()

drawhouse(t)

turtle.done()

main()

User Thomas Darvik
by
3.1k points