python中goto的用法 |
您所在的位置:网站首页 › goto指令含义 › python中goto的用法 |
Python的一个优点是可以很容易地在解释器中测试小部分代码。让我们用翻译来检查两边的长度:In [82]: number_of_shapes = 4 In [83]: for shape in range(1, number_of_shapes + 1): ....: print(20 + shape * 10) 30 40 50 60 哦。现在我们可以清楚地看到forward(20 + shape * 10)不会使边的长度为20、40、60和80。注意,(20 + shape * 10)使边长增加了10。我们想增加20,所以使用(20 + shape * 20)代替: ^{pr2}$ 哦,我搞错了。没问题,我们只需要把整件事减少20:In [85]: for shape in range(1, number_of_shapes + 1): ....: print(shape * 20) ....: 20 40 60 80 啊,好多了。在 现在代码如下:import turtle number_of_shapes = 4 for shape in range(1, number_of_shapes+1): #Draw a square for sides in range(4): turtle.forward(20 + shape * 20) turtle.right(90) #Move to position of next square turtle.penup() turtle.goto(shape * 10, shape * 10) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |