注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
对前面的代码进行优化,用for,while,if,def实现:
画五角星
import turtle
turtle.color('yellow','yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
画同心圆
import turtle
for i in range(5):
turtle.up()
turtle.goto(0,-20*(i+1))
turtle.down()
turtle.circle(20*(i+1))
画太阳花
import turtle
turtle.color('red','blue')
turtle.begin_fill()
while True:
turtle.forward(200)
turtle.left(165)
if(abs(turtle.pos()))<1:
break
turtle.end_fill()
画五个角星
import turtle
turtle.color('yellow','yellow')
turtle.bgcolor('red')
def lmhgoto(x,y):
turtle.up()
turtle.goto(x,y)
turtle.down()
lmhgoto(-250,100)
turtle.begin_fill()
for i in range(5):
turtle.forward(125)
turtle.right(144)
turtle.end_fill()
lmhgoto(-90,120)
turtle.begin_fill()
for i in range(5):
turtle.forward(30)
turtle.left(144)
turtle.end_fill()
lmhgoto(-120,165)
turtle.begin_fill()
for i in range(5):
turtle.forward(30)
turtle.left(144)
turtle.end_fill()
lmhgoto(-95,65)
turtle.begin_fill()
for i in range(5):
turtle.forward(30)
turtle.right(144)
turtle.end_fill()
lmhgoto(-120,10)
turtle.begin_fill()
for i in range(5):
turtle.forward(30)
turtle.left(144)
turtle.end_fill()