def main():
turtle.setworldcoordinates(0, -20, 40, 80)
plotter = turtle.Turtle()
plotter.speed(0)
plotter.penup()
tplot(plotter, square)
screen = plotter.getscreen()
screen.exitonclick()
Create a file named turtleplot.py
in which you will write your code.
Write the following functions:
Write the following functions:
identity(x)
returns x.square(x)
returns the square of x.sum_to_n(n)
returns the sum 1 + 2 + … + n,
calculated using a for loop. sum_odds(n)
returns the sum 1 + 3 + 5 + … + Kn,
where Kn is the nth odd number,
calculated using a for loop.
sum_frac(n)
returns the sum 6/1 + 6/2 + 6/4 + 6/8
+ 6/16 + … + 6/2n,
calculated using a for loop.Write a function tplot(tortoise, f)
that takes as
parameters
a turtle and
a function, and uses the turtle to plot the function.
Here's an algorithm outline:
Turtle
module documentation if you're not familiar
with the goto
and dot
methods.)Write a main
function with the following code.
Remember to import the turtle
module at the top of your
file.
def main():
turtle.setworldcoordinates(0, -20, 40, 80)
plotter = turtle.Turtle()
plotter.speed(0)
plotter.penup()
tplot(plotter, square)
screen = plotter.getscreen()
screen.exitonclick()
If your tplot
function is working, you should see
Plotter the turtle
plotting the points of the square
function:
Try plotting the other functions as well by changing the argument to
tplot
from square
to another function.
You can also pass in built-in functions from math
, for
example passing math.sin
looks like this:
Modify the main
function so that it plots several functions in
different colors as follows:
black |
identity
|
red |
square |
orange |
math.log2 |
blue |
sum_to_n |
indigo |
sum_odds |
purple |
sum_frac |
Submit one file, turtleplot.py
,
using the turnin form.
- 1 ptsum_to_n
and sum_odds
- 3 pts sum_frac - 2 points
tplot
function - 3 ptsmain
function and comparing plots - 2 pointsCreated September 19, 2016
Last revisedSeptember 22, 2016, 01:59:11 PM PDT
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.