条件、循环、函数定义 练习
- 注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
- 对前面的代码进行优化,用for,while,if,def实现:
- 画五角星
- 画同心圆
- 画太阳花
- 画五个角星
1.答:
import turtle
turtle.color('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(200)
turtle.left(144)
turtle.end_fill()
2.答:
import turtle
for i in range(4):
turtle.circle(20*(i+1))
turtle.up()
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.down()
3.答:
import turtle
turtle.color('red')
turtle.fillcolor('yellow')
turtle.begin_fill()
while True:
turtle.forward(200)
turtle.left(190)
if abs(turtle.pos())<1:
break
turtle.end_fill()
4.答:
import turtle
turtle.bgcolor('red')
turtle.color('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.up()
turtle.goto(-200,50)
turtle.down()
for i in range(5):
turtle.forward(200)
turtle.left(144)
turtle.end_fill()
turtle.up()
turtle.left(35)
turtle.forward(300)
turtle.down()
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.left(144)
turtle.end_fill()
turtle.up()
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.down()
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.left(144)
turtle.end_fill()
turtle.up()
turtle.right(120)
turtle.forward(100)
turtle.left(120)
turtle.down()
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.left(144)
turtle.end_fill()
turtle.up()
turtle.right(150)
turtle.forward(100)
turtle.left(150)
turtle.down()
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(50)
turtle.left(144)
turtle.end_fill()