TurtleWorld Exercises

1. Write a function called square that takes a parameter named t, which is a turtle. It should use the turtle to draw a square.

from TurtleWorld import *
def square(t):
    for i in range(4):
        fd(t,100)
        lt(t)

TurtleWorld()
bob = Turtle()
square(bob)

2. Add a parameter , named length, to square. Modify the body so length of the sides is length, and then modify the function call to provide a second argument.

from TurtleWorld import *
def square(t,length):
    for i in range(4):
        fd(t,length)
        lt(t)

TurtleWorld()
bob = Turtle()
square(bob,50)

3. Provide an argument to that specify the numbers of degrees. For example lt(bob,45) turns bob 45 degrees to the left.

from TurtleWorld import *
def polygon(t,length,degree):
    n = int(360/degree)
    for i in range(n):
        fd(t,length)
        lt(t,degree)

TurtleWorld()
bob = Turtle()
polygon(bob,100,60)

4. Write a function called circle that draws an approximative circle.

from TurtleWorld import *
import math
def circle(t,r):
    for i in range(360):
        fd(t,int((2*math.pi*r)/360))
        lt(t,1)
        
TurtleWorld()
bob = Turtle()
bob.delay = 0.01 # speed up bob to draw the circle
circle(bob,100)

 

 

 

 

 

 

 

 

    

 

posted @ 2014-04-24 16:13  平静缓和用胸音说爱  阅读(248)  评论(0编辑  收藏  举报