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

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

时间:2017-09-13 23:21:12      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:tle   pre   实现   bre   col   函数   方式   begin   tool   

1.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。

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

   a.画五角星

技术分享
import turtle
turtle.color(‘yellow‘)
turtle.begin_fill()
for i in range(5):
    turtle.forward(120)
    turtle.right(144)
turtle.end_fill()
turtle.done()
技术分享

技术分享

   b.画同心圆

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

技术分享

  c.画太阳花

 

技术分享
from turtle  import *
speed(20)
shape(‘turtle‘)
color(‘green‘,‘yellow‘)
begin_fill()
while True:
    forward(200)
    right(170)
    if(abs(pos()))<1:
        break
end_fill()
hideturtle()
done()
技术分享

技术分享

  d.画五个五角星

 

技术分享
import turtle
turtle.speed(20)
turtle.color(‘yellow‘)
turtle.bgcolor(‘red‘)
turtle.fillcolor(‘yellow‘)
def wu_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def hong_draw5(r):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.right(144)
    turtle.end_fill()

wu_goto(-350,180)
hong_draw5(150)
turtle.left(50)

wu_goto(-150,250)
hong_draw5(50)
turtle.left(50)

wu_goto(-100,170)
hong_draw5(50)
turtle.left(45)

wu_goto(-80,85)
hong_draw5(50)
turtle.left(50)

wu_goto(-130,35)
hong_draw5(50)
turtle.left(50)

turtle.hideturtle()
turtle.done()
技术分享

 技术分享

 

  e.画◇花瓣的太阳花

 

技术分享
import turtle
turtle.speed(100)
turtle.color(‘green‘)
turtle.fillcolor(‘red‘)
turtle.begin_fill()
for i in range(1,37):
    for i in range(1,3):
        turtle.forward(100)
        turtle.right(45)
        turtle.forward(100)
        turtle.right(135)
    turtle.left(20)
turtle.end_fill()
turtle.right(90)
turtle.forward(400)
turtle.hideturtle()

技术分享

 

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

标签:tle   pre   实现   bre   col   函数   方式   begin   tool   

原文地址:http://www.cnblogs.com/xyjie/p/7517889.html

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