think python学习心得-(2)接口设计

时间:2022-05-06 16:49:46

TurtleWorld模块的下载与安装:

下载地址就是:http://thinkpython.com/swampy

安装方法把压缩包解压之后,用cmd cd到解压的安装目录下,运行:

python setup.py install
安装后就可以from swampy.TurtleWorld import *啦!

练习4.3

1-

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

def square(t):
for i in range(4):
fd(t,100)
lt(t)

square(bob)
wait_for_user()

2-

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

def square(t,length):
for i in range(4):
fd(t,length)
lt(t)

square(bob,50)
wait_for_user()

3-

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

def square(t,length,n):
for i in range(n):
fd(t,length)
lt(t,360/n)

square(bob,50,6)
wait_for_user()

4-

太简单了不写了

5-

直接用math包

画六边形:

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()

def square(t,length,n):
for i in range(n):
fd(t,length)
lt(t,360/n)

for i in range(6):
square(bob,50,3)
lt(bob,60)

wait_for_user()