2012년 4월 7일 토요일

Exercise 4.1-Answer

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

Write a non-fruitful function to draw a square. Use it in a program to draw the image shown below. Assume each side is 20 units. (Hint: notice that the turtle has already moved away from the ending point of the last square when the program ends.)

_images/five_squares.png

 

  1: # -*- coding: UTF-8 -*-
  2: #!/usr/bin/env python
  3: 
  4: import turtle
  5: 
  6: def createWindow(bgcolor, title):
  7:     w = turtle.Screen()
  8:     w.bgcolor(bgcolor)
  9:     w.title(title)
 10:     return w
 11: 
 12: def createTurtle(color, size):
 13:     t = turtle.Turtle()
 14:     t.pensize(size)
 15:     t.color(color)
 16:     return t
 17: 
 18: def jump(t, l):
 19:     t.penup()
 20:     t.forward(l)
 21:     t.pendown()
 22: 
 23: def draw_square(t, l):
 24:     for i in range(4):
 25:         t.forward(l)
 26:         t.left(90)
 27:     jump(t, l * 2)
 28: 
 29: 
 30: wn = createWindow("lightgreen", "Exercises 4.1")
 31: tess = createTurtle("hotpink", 3)
 32: 
 33: for i in range(5):
 34:     draw_square(tess, 20)
 35: 
 36: wn.mainloop()
 37: 

댓글 없음:

댓글 쓰기