条件、循环、函数定义 练习

1.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。2.对前面的代码进行优化,用for,while,if,def实现

A.画五角星

>>> import turtle
>>> turtle.color('yellow')
>>> turtle.bgcolor('red')
>>> turtle.hideturtle()
>>> turtle.beginfill()
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    turtle.beginfill()
AttributeError: module 'turtle' has no attribute 'beginfill'
>>> turtle.begin_fill()
>>> turtle.forward(99)
>>> turtle.right(144)
>>> turtle.forward(99)
>>> turtle.right(144)
>>> turtle.forward(99)
>>> turtle.right(144)
>>> turtle.forward(99)
>>> turtle.right(144)
>>> turtle.forward(99)
>>> turtle.end_fill()

B.画同心圆

import turtle

for i in range(5):
    turtle.up()
    turtle.goto(0,-25*(i+1))
    turtle.down()
    turtle.circle(25*(i+1))

C.画太阳花

import turtle
turtle.pensize(3)
turtle.pencolor("red")
turtle.fillcolor("yellow")
turtle.begin_fill()
while True:
    turtle.forward(300)
    turtle.left(150)
    if(abs(turtle.pos()))<1:
        break
turtle.end_fill()
turtle.hideturtle()

D.画五个五角星

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()

E.画菱形花瓣的太阳花

import turtle
turtle.speed(12)
turtle.fillcolor("yellow")
turtle.pencolor("red")
turtle.begin_fill()
def draw_leng():
    for i in range(1,3):
        turtle.forward(75)
        turtle.right(45)
        turtle.forward(75)
        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()

 

posted @ 2017-09-13 17:28  066谢平坚  阅读(332)  评论(0)    收藏  举报