Array of Stars

Back to index

Inspect this next example carefully and get it up and running. Notice that there are embedded for-loops in this example.
#!/usr/bin/python import turtle t=turtle.Pen() t.speed(10) t.screen.bgcolor("green") t.pencolor("black") t.pensize(3) for y in range(-80,82,40): for x in range(-80,82,40): t.penup() t.setx(x) t.sety(y) t.pendown() t.seth(72) for s in range(5): t.right(144) t.forward(30) turtle.mainloop()
Indenting is extremely important in Python. When it comes to for-loops anything contained within a for-loop is indented below it. Therefore, for s in range(5): gets executed for each iteration of for x in range(-80,82,40):, which in turn is executed during each iteration of for y in range(-80,82,40):. How many stars are produced by the sample program? That's how many times the for s in range(5): loop gets executed. Just for grins and giggles, change the 82 in the for-loops for x and y to 80 and do a recount on the stars. Why does the number of stars change?
ASSIGNMENT:

Experiment to figure out how to make stars with various numbers of points. Try to make seven, ten, eleven, and twelve pointed stars. Is there a difference in the way you go about making stars with odd and even numbers of points?

Once you have spent at least fifteen minutes playing around with multi-pointed stars and have successfully figured out how to make a seven point star, continue to the next part of the assignment.

Your assignment is to make a 15x15 array of seven-pointed stars.