条件、循环、函数定义 练习
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()