Notes: constant change

posted by: Ms. Martin 16 February 2011 No Comment

You should now feel comfortable with basic turtle graphics functions, defining your own functions, the print function and using for loops for repetition.

Recall that a for loop is defined with the code for i in range(<num>).  i gets a new value with each repetition of the loop body, starting at 0.  To verify this, write and run the following code:

for i in range(10):
    print(i)

You can use i to create changing patterns in Turtle Graphics.  For example, how would we draw something like the following?

from turtle import *

for i in range(7):
        down()
        left(90)
	forward(i * 10)
	forward(-(i * 10))
	up()
	right(90)
	forward(50)

We can use a multiple of i to result in constant change.

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 3.00 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>