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

1.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。

2.对前面的代码进行优化,用for,while,if,def实现:

a.画五角星

import turtle

turtle.color("yellow")
turtle.bgcolor("red")
turtle.fillcolor("yellow")

def star_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def star_draw(r):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.left(144)
    turtle.end_fill()

star_goto(-300,105)
star_draw(100)

b.画同心圆

import turtle
turtle.color('red')
for i in range(5):
    turtle.up()
    turtle.goto(0,-20*(i+1))
    turtle.down()
    turtle.circle(20*(i+1))

c.画太阳花

import turtle 
turtle.color('green')
turtle.fillcolor('yellow')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if(abs(turtle.pos()))<1:
       break

turtle.end_fill()
done()

d.画五个五角星

import turtle

turtle.color("yellow")
turtle.bgcolor("red")
turtle.fillcolor("yellow")

def star_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def star_draw(r):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.left(144)
    turtle.end_fill()

star_goto(-300,105)
star_draw(100)

star_goto(-200,200)
star_draw(50)

star_goto(-150,150)
star_draw(50)

star_goto(-150,100)
star_draw(50)

star_goto(-200,50)
star_draw(50)

e.画◇花瓣的太阳花。

import turtle
def my_lx(brad):
    brad.forward(100)
    brad.right(45)
    brad.forward(100)
    brad.right(135)

def my_art():
    window=turtle.Screen()
    window.bgcolor("green")
    brad=turtle.Turtle()
    brad.shape("turtle")
    brad.color("orange")
    brad.speed('fastest')
    for i in range(1,37):
        my_lx(brad)
        my_lx(brad)
        brad.left(10)
    brad.right(90)
    brad.forward(300)
my_art()

原文地址:https://www.cnblogs.com/00js/p/7515589.html