条件、循环、函数定义 练习
- 注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
- 对前面的代码进行优化,用for,while,if,def实现:
- 画五角星
import turtle for i in range(5): turtle.forward(100) turtle.left(144) turtle.speed(8)
- 画同心圆
import turtle for i in range(3): turtle.up() turtle.goto(0,-30*(i+1)) turtle.down() turtle.circle(30*i)
- 画太阳花
import turtle turtle.color('red','orange') turtle.speed(10) turtle.begin_fill() while True: turtle.forward(150) turtle.left(170) if(abs(turtle.pos()))<1: turtle.end_fill() break
- 画五个角星
import turtle turtle.color('yellow') turtle.bgcolor('red') turtle.fillcolor('yellow') def mygoto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def ye(r): turtle.begin_fill() for i in range(5): turtle.forward(r) turtle.right(144) turtle.end_fill() mygoto(-320,250) ye(100) mygoto(-160,300) ye(30) mygoto(-110,250) ye(30) mygoto(-110,190) ye(30) mygoto(-160,140) ye(30)
- 画五角星