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

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

画五角星

方法一:

import turtle

for i in range(5):
print(i)
turtle.forward(100)
turtle.right(144)

方法二:

import turtle

while True:
turtle.forward(160)
turtle.right(144)
if(abs(turtle.pos()))<1:
break

 

 

画同心圆

import turtle

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

 

 

画太阳花

import turtle

turtle.color('white')
turtle.fillcolor('white')
turtle.bgcolor('blue')

turtle.begin_fill()

while True:
turtle.forward(200)
turtle.left(170)
if (abs(turtle.pos()))<1:
break
turtle.end_fill()

 

画五个角星

import turtle

turtle.color('white')
turtle.fillcolor('white')
turtle.bgcolor('blue')

turtle.begin_fill()

def myrang(z):

for i in range(5):
print(i)
turtle.forward(100)
turtle.right(144)
print(myrang);
return;


def mygoto(x,y):

turtle.up()
turtle.goto(x,y)
turtle.down()
print(mygoto);
return;


mygoto(-200,200)
myrang(5)

mygoto(-100,100)
myrang(5)

mygoto(0,0)
myrang(5)

mygoto(-100,-100)
myrang(5)

mygoto(-200,-200)
myrang(5)


turtle.end_fill()

posted on 2017-09-12 15:04  152陈斯璐  阅读(94)  评论(0编辑  收藏  举报