Using python. Define functions to draw rectangles, squares and triangles. All fu
ID: 3889548 • Letter: U
Question
Using python.
Define functions to draw rectangles, squares and triangles. All functions must be defined with parameters.
Define a function that uses a turtle object to fill in a rectangle with diagonal lines from the bottom left corner to the top right corner. The lines need to be equally spaced. The function needs to define these parameters: the turtle object, the color of the lines, the x and y coordinates of the bottom left corner, the width and height of the rectangle, and the space between the lines. Before the function returns the turtle needs to be at the position and heading it was in before the function was called. Do not use conditional statements.
Explanation / Answer
“Turtle” is a python feature like a drawing board, which lets you command a turtle to draw all over.
You can use functions like turtle.forward(...) and turtle.left(...) which can move the turtle around.
Before you can use turtle, you have to import it. We recommend playing around with it in the interactive interpreter first, as there is an extra bit of work required to make it work from files,to your terminal and type:
import turtle
turtle.forward(25)
turtle.left(30)
1The turtle.forward(...) function tells the turtle to move forward by the given distance. turtle.left(...) takes a number of degrees which you want to rotate to the left. There is also turtle.backward(...) and turtle.right(...), too.
The standard turtle is just a triangle. That’s no fun! Let’s make it a turtle instead with the turtle.shape() command:
turtle.shape("turtle")
you put the commands into a file, you might have recognized that the turtle window vanishes after the turtle finished its movement. (That is because Python exits when your turtle has finished moving. Since the turtle window belongs to Python, it goes away as well.) To prevent that, just put turtle.exitonclick() at the bottom of your file. Now the window stays open until you click on it:
import turtle
turtle.shape("turtle")
turtle.forward(25)
turtle.exitonclick()
A Square you will probably need a right angle, which is 90 degrees.
Solution
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
Draw a rectangle
Solution
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
The angles between the individual squares.
turtle.left(20)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.left(30)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.left(40)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.