构建自己的循环
内建函数range(n):构建一个0到n-1的整数数字列表
class range
(stop)
class range
(start, stop[, step])
内建函数list():构建列表
for语句:
for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite]
用for循环生成四个花瓣
# Rosette4.py
import turtle
t = turtle.Pen()
for x in range(4):
t.circle(100)
t.left(90)
用for循环生成六个花瓣
# Rosette6.py
import turtle
t = turtle.Pen()
for x in range(6):
t.circle(100)
t.left(60)
增加用户输入功能
获取输入的数字字符串:turtle.
numinput
(title, prompt, default=None, minval=None, maxval=None)
# RosetteGoneWild.py
import turtle
t = turtle.Pen()
# Ask the user for the number of circles in their rosette, default to 6
number_of_circles = int(turtle.numinput("Number of circles",
"How many circles in your rosette?", 6))
for x in range(number_of_circles):
t.circle(100)
t.left(360/number_of_circles)
游戏循环和while循环
while语句:适合循环次数不确定的情况
while_stmt ::= "while" assignment_expression ":" suite ["else" ":" suite]
# SayOurNames.py - lets everybody print their name on the screen
# Ask the user for their name
name = input("What is your name? ")
# Keep printing names until we want to quit
while name != "":
# Print their name 100 times
for x in range(100):
# Print their name followed by a space, not a new line
print(name, end = " ")
print() # After the for loop, skip down to the next line
# Ask for another name, or quit
name = input("Type another name, or just hit [ENTER] to quit: ")
print("Thanks for playing!")
家庭成员螺旋线
# SpiralFamily.py - prints a colorful spiral of names
import turtle # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange",
"purple", "white", "brown", "gray", "pink" ]
family = [] # Set up an empty list for family names
# Ask for the first name
name = turtle.textinput("My family",
"Enter a name, or just hit [ENTER] to end:")
# Keep asking for names
while name != "":
# Add their name to the family list
family.append(name)
# Ask for another name, or end
name = turtle.textinput("My family",
"Enter a name, or just hit [ENTER] to end:")
# Draw a spiral of the names on the screen
for x in range(100):
t.pencolor(colors[x%len(family)]) # Rotate through the colors
t.penup() # Don't draw the regular spiral lines
t.forward(x*4) # Just move the turtle on the screen
t.pendown() # Draw the next family member's name
t.write(family[x%len(family)], font = ("Arial", int((x+4)/4), "bold") )
t.left(360/len(family) + 2) # Turn left for our spiral
整合-病毒式的螺旋线
# ViralSpiral.py - a spiral of spirals!
import turtle
t = turtle.Pen()
t.penup()
turtle.bgcolor("black")
# Ask the user for the number of sides, default to 4, min 2, max 6
sides = int(turtle.numinput("Number of sides",
"How many sides in your spiral of spirals? (2-6)", 4,2,6))
colors = ["red", "yellow", "blue", "green", "purple", "orange"]
# Our outer spiral loop
for m in range(100):
t.forward(m*4)
position = t.position() # Remember this corner of the spiral
heading = t.heading() # Remember the direction we were heading
print(position, heading)
# Our "inner" spiral loop
# Draws a little spiral at each corner of the big spiral
for n in range(int(m/2)):
t.pendown()
t.pencolor(colors[n%sides])
t.forward(2*n)
t.right(360/sides - 2)
t.penup()
t.setx(position[0]) # Go back to the big spiral's x location
t.sety(position[1]) # Go back to the big spiral's y location
t.setheading(heading) # Point in the big spiral's heading
t.left(360/sides + 2) # Aim at the next point on the big spiral
编程挑战
螺旋线玫瑰花瓣
修改病毒式的螺旋线程序,将小的螺旋线替换为玫瑰花瓣,运行结果如下图