Question
The PYTHON programming assignment for this unit has two functions. 1. Use these two functions as models for the functions you are to write. You
The PYTHON programming assignment for this unit has two functions.
1. Use these two functions as models for the functions you are to write. You will be removing code from main() and putting it in several separate functions. The behavior of the program will not change throughout the exercise, so you can always tell if your program is correct.
2. Use the non-fruitful function drawRect as a model. Write another non-fruitful function that moves a turtle forward without drawing a line. The function is defined as
3. def skipForward(t, len): """ Moves the turtle t forward len units without drawing anything. """ and called with a statement like skipForward(alex, 20) The contents of the function are to lift the pen, move the turtle, then lower the pen.
4. Write another non-fruitful function skipTo, which is defined as
5. def skipTo(t, x, y): """ Moves the turtle t to coordinates (x,y) without drawing anything. """ and is called with skipTo(alex, xpos, ypos) Make sure to use the parameters x and y in the body of this function. Next, you'll work with fruitful functions. Look at the function to calculate the next y-position for drawing a set of shapes. This function is called near the bottom of the program: ypos = next_y_position(ypos, width)
6. Using this function as a model, add a fruitful function to calculate the perimeter of a rectangle. The function is defined with def perimeter(len,wid): """ Returns the perimeter of rectangle with sides len and wid. """ and it is called with the statement p = perimeter(length, width) Make sure you use the parameters len and wid in the body of the function.
7. Again, using this function as a model, add a fruitful function to calculate the value math.sqrt(length*width/math.pi) which appears near the end of the program. The function should be defined as def circ_rad(len, wid): """ Returns the radius of a circle with an area of len * wid """ Mimic the call from what you did in the previous step.
8. Make sure that your main function is calling each of the non-fruitful functions at least twice.
------------------------------------------------------------------------------------------------------------------------------------------
# Draws 3 sets of shapes of varying lengths
# Draw a rectangle
# Draw a line the length of the rectangle's perimeter
# Draw a circle with the same area as the rectangle
import turtle
import math
def next_y_position(y,ht):
""" Returns the next y-position, given current position y and height
ht """
next = y + ht + 30
return next
def drawRect(t, len, wid):
""" Draws a rectangle using turtle t with sides len and wid """
for side in [len, wid, len, wid]:
t.forward(side)
t.left(90)
def main():
# named constants
screen_size = 500
screen_startx = 60 # x coordinate of the left edge of the graphics
window
# Set up the window and its attributes
wn = turtle.Screen()
wn.bgcolor("lightblue")
wn.setup(screen_size, screen_size, screen_startx, 0)
alex = turtle.Turtle()
# Initial turtle position near left edge, toward the bottom
xpos = -screen_size/2 + 20
ypos = -screen_size/2 + 50
alex.up()
alex.goto(xpos,ypos)
alex.down()
# y-dimension of each rectangle
width = 50
# draw three sets of shapes - same width(y-dimension) but different
lengths
for length in [25, 50, 75]:
# Draw the rectangle
drawRect(alex, length, width)
# Move a little to the right of the rectangle
alex.up()
alex.forward(length)
alex.forward(10)
alex.down()
# Draw the line the same length as the perimeter of the rectangle
per = 2 * (length + width)
alex.forward(per)
# Move a little to the right of the line
alex.up()
alex.forward(20)
alex.down()
# Draw a circle with the same area as the rectangle
alex.begin_fill()
rad = math.sqrt(length*width/math.pi)
alex.circle(rad)
alex.end_fill()
# Next vertical position for a set of shapes
ypos = next_y_position(ypos, width)
# Put turtle to left side of screen at correct height
alex.up()
alex.goto(xpos,ypos)
alex.down()
# Run the main function. This should be the last statement in the file.
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started