Write a function called square that takes a parameter named t, which is a turtle. Itshould use the turtle to draw a square.Write a function call that passes bob as an argument to square, and then run theprogram again.2. Add another parameter, named length, to square. Modify the body so length of thesides is length, and then modify the function call to provide a second argument. Runthe program again. Test your program with a range of values for length.3. Make a copy of square and change the name to polygon. Add another parameternamed n and modify the body so it draws an n-sided regular polygon. Hint: Theexterior angles of an n-sided regular polygon are 360/n degrees.4. Write a function called circle that takes a turtle, t, and radius, r, as parameters andthat draws an approximate circle by calling polygon with an appropriate length andnumber of sides. Test your function with a range of values of r.Hint: figure out the circumference of the circle and make sure that length * n =circumference.5. Make a more general version of circle called arc that takes an additional parameterangle, which determines what fraction of a circle to draw. angle is in units of degrees,so when angle=360, arc should draw a complete circle.
Question
Write a function called square that takes a parameter named t, which is a turtle. Itshould use the turtle to draw a square.Write a function call that passes bob as an argument to square, and then run theprogram again.2. Add another parameter, named length, to square. Modify the body so length of thesides is length, and then modify the function call to provide a second argument. Runthe program again. Test your program with a range of values for length.3. Make a copy of square and change the name to polygon. Add another parameternamed n and modify the body so it draws an n-sided regular polygon. Hint: Theexterior angles of an n-sided regular polygon are 360/n degrees.4. Write a function called circle that takes a turtle, t, and radius, r, as parameters andthat draws an approximate circle by calling polygon with an appropriate length andnumber of sides. Test your function with a range of values of r.Hint: figure out the circumference of the circle and make sure that length * n =circumference.5. Make a more general version of circle called arc that takes an additional parameterangle, which determines what fraction of a circle to draw. angle is in units of degrees,so when angle=360, arc should draw a complete circle.
Solution
- To write a function called square that takes a parameter named t, which is a turtle, follow these steps:
Step 1: Define the function called square with the parameter t. Step 2: Inside the function, use the turtle t to draw a square. You can use the turtle's methods like forward, right, and left to move the turtle and draw the square.
Here's an example implementation of the square function:
def square(t):
for i in range(4):
t.forward(100) # Assuming the length of each side is 100 units
t.right(90)
- To add another parameter named length to the square function, follow these steps:
Step 1: Modify the function definition of square to include the parameter length. Step 2: Inside the function, replace the hardcoded length value with the length parameter.
Here's an example implementation of the modified square function:
def square(t, length):
for i in range(4):
t.forward(length)
t.right(90)
To test the program with different values of length, you can call the square function with different arguments. For example:
import turtle
bob = turtle.Turtle()
square(bob, 50) # Draws a square with side length 50
square(bob, 200) # Draws a square with side length 200
- To make a copy of the square function and change the name to polygon, follow these steps:
Step 1: Define a new function called polygon with the same parameters as the square function (t and length). Step 2: Inside the function, modify the body to draw an n-sided regular polygon instead of a square. You can use the formula for the exterior angles of a regular polygon to calculate the angle to turn the turtle.
Here's an example implementation of the polygon function:
def polygon(t, length, n):
angle = 360 / n
for i in range(n):
t.forward(length)
t.right(angle)
- To write a function called circle that takes a turtle t and radius r as parameters and draws an approximate circle, follow these steps:
Step 1: Define the function called circle with the parameters t and r. Step 2: Calculate the circumference of the circle using the formula 2 * pi * r, where pi is a mathematical constant. Step 3: Determine the number of sides needed to approximate the circle by dividing the circumference by the desired length of each side. Step 4: Call the polygon function with the turtle t, the calculated length, and the number of sides.
Here's an example implementation of the circle function:
import math
def circle(t, r):
circumference = 2 * math.pi * r
length = circumference / 360 # Assuming each side corresponds to 1 degree
n = int(circumference / length)
polygon(t, length, n)
To test the circle function, you can call it with different values of r. For example:
import turtle
bob = turtle.Turtle()
circle(bob, 50) # Draws an approximate circle with radius 50
circle(bob, 100) # Draws an approximate circle with radius 100
- To make a more general version of the circle function called arc that takes an additional parameter angle, follow these steps:
Step 1: Define the function called arc with the parameters t, r, and angle. Step 2: Calculate the circumference of the arc using the formula (angle / 360) * 2 * pi * r. Step 3: Determine the number of sides needed to approximate the arc by dividing the circumference by the desired length of each side. Step 4: Call the polygon function with the turtle t, the calculated length, and the number of sides.
Here's an example implementation of the arc function:
import math
def arc(t, r, angle):
circumference = (angle / 360) * 2 * math.pi * r
length = circumference / 360 # Assuming each side corresponds to 1 degree
n = int(circumference / length)
polygon(t, length, n)
To test the arc function, you can call it with different values of r and angle. For example:
import turtle
bob = turtle.Turtle()
arc(bob, 50, 90) # Draws an approximate quarter circle with radius 50
arc(bob, 100, 180) # Draws an approximate half circle with radius 100
Similar Questions
Write a function called square that takes a parameter named t, which is a turtle. Itshould use the turtle to draw a square.Write a function call that passes bob as an argument to square, and then run theprogram again.
Write a function called circle that takes a turtle, t, and radius, r, as parameters andthat draws an approximate circle by calling polygon with an appropriate length andnumber of sides. Test your function with a range of values of r.Hint: figure out the circumference of the circle and make sure that length * n =circumference.
Write a function call that passes bob as an argument to square, and then run theprogram again.
Select the correct answerWhat will be the output of the following Python code?import turtle as ttfor k in range(0,4): tt.forward(200) tt.left(90) tt.penup()tt.left(90)tt.forward(200)for a in range(0,4): tt.forward(100) tt.left(90)Options1 square2 squares, at a separation of 100 units, without a line joining themError2 squares, at a separation of100 units, joined by a straight line
Which of the following is NOT an example of a built-in function in Python Turtle programming?(1 Point)print( )range( )draw_circle( )len( )
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.