python-4 接口

2020-04-13  本文已影响0人  巴巴11

turtle 模块,可以使用海龟图形(turtle graphics)绘制图像。

import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
bob.fd(100)
# fd 向前
# bk 向后
# 向左转的 lt ,向右转的 rt,lt 和 rt 这两个方法接受的实参是角度

关键字实参:
如果一个函数有几个数字实参,很容易忘记它们是什么或者它们的顺序。
在这种情况下, 在实参列表中加入形参的名称是通常是一个很好的办法:
polygon(bob, n=7, length=70)

文档字符串:
文档字符串(docstring)是位于函数开始位置的一个字符串, 解释了函数的接口。

def polyline(t, n, length, angle):
    """Draws n line segments with the given length and
    angle (in degrees) between them.  t is a turtle.
    """
    for i in range(n):
        t.fd(length)
        t.lt(angle)
上一篇 下一篇

猜你喜欢

热点阅读