2012년 4월 7일 토요일

Exercise 4.3-Answer

from: http://openbookproject.net/thinkcs/python/english3e/functions.html#exercises

Write a non-fruitful function draw_poly(t, n, sz) which makes a turtle draw a regular polygon. When called with draw_poly(tess, 8, 50), it will draw a shape like this:

_images/regularpolygon.png

 

  1: # -*- coding: UTF-8 -*-
  2: #!/usr/bin/env python
  3: import turtle
  4: 
  5: def createWindow(bgcolor, title):
  6:     w = turtle.Screen()
  7:     w.bgcolor(bgcolor)
  8:     w.title(title)
  9:     return w
 10: 
 11: def createTurtle(color, size):
 12:     t = turtle.Turtle()
 13:     t.pensize(size)
 14:     t.color(color)
 15:     return t
 16: 
 17: def draw_poly(t, n, sz):
 18:     for i in range(n):
 19:         t.forward(sz)
 20:         t.left(360 / n)
 21: 
 22: wn = createWindow("lightgreen", "Exercises 4.3")
 23: tess = createTurtle("hotpink", 3)
 24: 
 25: draw_poly(tess, 8, 50)
 26: 
 27: wn.mainloop()
 28: 

댓글 없음:

댓글 쓰기