条件、循环、函数定义 练习
1.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
2.对前面的代码进行优化,用for,while,if,def实现:
1.画五角星
import turtle turtle.pencolor("red") turtle.pensize(5) turtle.fillcolor("yellow") turtle.begin_fill() for i in range(5): turtle.forward(100) turtle.right(144) turtle.end_fill()
turtle.hideturtle()
2.画同心圆
import turtle for i in range(5): turtle.circle(20*(i+1)) turtle.up() turtle.goto(0,-20*(i+1)) turtle.down() turtle.hideturtle()
3.画太阳花
import turtle turtle.pensize(3) turtle.pencolor("red") turtle.fillcolor("yellow") turtle.begin_fill() while True: turtle.forward(300) turtle.left(170) if(abs(turtle.pos()))<1: break turtle.end_fill() turtle.hideturtle()
4.画五个角星
import turtle turtle.bgcolor('red') def m_goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def m_draw(r): turtle.pencolor('yellow') turtle.fillcolor('yellow') turtle.begin_fill() for i in range(5): turtle.forward(r) turtle.right(144) turtle.end_fill() m_goto(-350,180) m_draw(150) m_goto(-175,250) turtle.left(50) m_draw(50) m_goto(-100,173) turtle.left(44) m_draw(50) m_goto(-70,80) turtle.left(50) m_draw(50) m_goto(-130,50) turtle.left(50) m_draw(50) turtle.hideturtle() turtle.done()
5.画◇花瓣的太阳花。
import turtle turtle.speed(10) turtle.fillcolor("pink") turtle.pencolor("red") turtle.begin_fill() def draw_leng(): for i in range(1,3): turtle.forward(100) turtle.right(45) turtle.forward(100) turtle.right(135) for i in range(1,37): draw_leng() turtle.right(10) turtle.end_fill() turtle.right(90) turtle.forward(300) turtle.hideturtle()
附加一个小心心♥~笔芯。
import turtle turtle.speed(10) def xin(): for i in range(200): turtle.right(1) turtle.forward(1) turtle.color('red','pink') turtle.begin_fill() turtle.left(140) turtle.forward(111.65) xin() turtle.left(120) xin() turtle.forward(111.65) turtle.end_fill() turtle.hideturtle()