码迷,mamicode.com
首页 > 其他好文 > 详细

条件、循环、函数定义 练习

时间:2017-09-12 21:58:18      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:down   epo   red   i+1   bre   分享   rtl   条件   goto   

前面的代码进行优化,用for,while,if,def实现:

1.画五角星

import turtle
for i in range(5):
    turtle.forward(100);
    turtle.right(144);

技术分享

2.画同心圆

import turtle
def circle(x):
    turtle.up()
    turtle.goto(0,-x)
    turtle.down()
    turtle.circle(x+10)
for i in range(5):
    circle((i+1)*20)

技术分享

3.画太阳花

import turtle
turtle.begin_fill()
turtle.fillcolor(‘pink‘)
turtle.speed(100)
while True:
    turtle.forward(200)
    turtle.left(170)
    if(abs(turtle.pos()))<1:
        turtle.end_fill()
        break

技术分享

4.画五个角星

import turtle
turtle.setup(600,400,0,0)
turtle.color("yellow")
turtle.bgcolor("red")
turtle.fillcolor(‘yellow‘)
turtle.speed(100)
turtle.begin_fill()
def reposi(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

reposi(-250,75)

for i in range(5):
    turtle.forward(100);
    turtle.right(144);
turtle.end_fill()

reposi(-130,120)
turtle.begin_fill()
for i in range(5):
    turtle.forward(30);
    turtle.right(144);
turtle.end_fill()

reposi(-110,80)
turtle.begin_fill()
for i in range(5):
    turtle.forward(30);
    turtle.right(144);
turtle.end_fill()

reposi(-110,40)
turtle.begin_fill()
for i in range(5):
    turtle.forward(30);
    turtle.right(144);
turtle.end_fill()

reposi(-130,0)
turtle.begin_fill()
for i in range(5):
    turtle.forward(30);
    turtle.right(144);
turtle.end_fill()

技术分享

条件、循环、函数定义 练习

标签:down   epo   red   i+1   bre   分享   rtl   条件   goto   

原文地址:http://www.cnblogs.com/mavenlon/p/7508594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!