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: 

Exercise 4.2-Answer

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

Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it.

_images/nested_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.backward(l)
 21:     t.right(90)
 22:     t.forward(l)
 23:     t.left(90)
 24:     t.pendown()
 25: 
 26: def draw_square(t, l):
 27:     for i in range(4):
 28:         t.forward(l)
 29:         t.left(90)
 30:     jump(t, 10)
 31: 
 32: wn = createWindow("lightgreen", "Exercises 4.2")
 33: tess = createTurtle("hotpink", 3)
 34: 
 35: for i in range(5):
 36:     draw_square(tess, 20 * (i + 1))
 37: 
 38: wn.mainloop()
 39: 

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: 

Exercise 3.2-Answer

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

Write a program to draw a face of a clock that looks something like this:

_images/tess_clock1.png

 

  1: # -*- coding: UTF-8 -*-
  2: #!/usr/bin/env python
  3: 
  4: import turtle           # turtle을 사용할 수 있도록 한다.
  5: 
  6: wn = turtle.Screen()
  7: wn.title("A face of a clock")
  8: wn.bgcolor("lightgreen")
  9: 
 10: alex = turtle.Turtle()
 11: alex.pensize(3)
 12: alex.speed(0)
 13: alex.shape("turtle")
 14: alex.color("blue")
 15: 
 16: for i in range(12):
 17:     alex.penup()
 18:     alex.forward(80)
 19:     alex.pendown()
 20:     alex.forward(10)
 21:     alex.penup()
 22:     alex.forward(30)
 23:     alex.stamp()
 24:     alex.backward(120)
 25:     alex.right(30)
 26: 
 27: alex.stamp()
 28: 
 29: wn.mainloop()           # 사용자가 윈도우를 닫을 때까지 기다린다.
 30: 

Exercise 3.1–Answer

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

Write a program to draw a shape like this:

_images/star.png

Hints:

  • Try this on a piece of paper, moving and turning your cellphone as if it was a turtle. Watch how many complete rotations your cellphone makes before you complete the star. Since each full rotation is 360 degrees, you can figure out the total number of degrees that your phone was rotated through. If you divide that by 5, because there are five points to the star, you’ll know how many degrees to turn the turtle at each point.
  • You can hide a turtle behind its invisibility cloak if you don’t want it shown. It will still draw its lines if its pen is down. The method is invoked as tess.hideturtle() . To make the turtle visible abain, use tess.showturtle() .

 

  1: # -*- coding: UTF-8 -*-
  2: #!/usr/bin/env python
  3: 
  4: import turtle           # turtle을 사용할 수 있도록 한다.
  5: 
  6: wn = turtle.Screen()
  7: wn.title("Star")
  8: 
  9: alex = turtle.Turtle()
 10: alex.pensize(5)
 11: alex.speed(1)
 12: 
 13: for i in range(5):
 14:     alex.forward(200)
 15:     alex.right(144)
 16: 
 17: alex.hideturtle()
 18: wn.mainloop()           # 사용자가 윈도우를 닫을 때까지 기다린다.
 19: