[:StartProgramming: （首页）开始编程之旅] 翻译自Lee Harr的[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html Start Programming]

1. StartProgramming-1-5 循环Loops

We made a square by performing the same two steps repeatedly -- once for each side of the square:

forward(100)
right(90)

It made it a bit easier that we could use the up-arrow to get back things we typed previously and so save ourselves some typing, but it was still easy to make a mistake and turn our square in to something not exactly what we planned.

There must be a simpler way, and there is.

Computers are great at repeating things over and over again without ever making any mistakes. We just need to know how to tell the computer what to do.

1.1. for

One way to tell a computer to repeat a set of steps is called a loop.

Here is how we tell pete to make a square using a loop:

for side in 1, 2, 3, 4:
forward(100)
right(90)

Go ahead and type in the first line for side in 1, 2, 3, 4: and hit [ENTER]

Notice that the interpreter does not come back with the >>> prompt this time. Instead it shows ...

This means it is waiting for more input before it can get started. You are saying "I want you to do something 4 times", and the computer comes back with "Ok. What should I do 4 times?"

In Python, the way we tell the computer which steps are part of the loop is by indenting.

It is sort of like an outline.

Use 4 spaces for each level of indentation. Go ahead now and hit 4 spaces, then type in the second line of the loop forward(100) and hit [ENTER]

The computer comes back with ... again.

It does not know if you are finished telling it what to do 4 times, or if there are more steps inside of the loop.

Turns out there is another step in the loop. Hit 4 more spaces, then type in the last line right(90) and hit [ENTER]

Again, the computer comes back with ... but this time we are finished with the steps in the loop.

To finish the loop, hit [ENTER] again.

pete should, very quickly, draw a square for you.

pete将会非常快的为你画一个正方形。

This loop is so simple that we can "unroll" the loop and take a look at exactly what is happening...

for side in 1, 2, 3, 4:
forward(100)
right(90)
side = 1
forward(100)
right(90)

side = 2
forward(100)
right(90)

side = 3
forward(100)
right(90)

side = 4
forward(100)
right(90)

These two pieces of code are equivalent. The result will be exactly the same.

Two things to notice here.

First, the variable side is never used inside of the loop. You might have named it differently, and it would not make much difference. Other reasonable names might have been count or even i or x. You do have to be a bit careful though, as naming the variable forward would cause problems.

Second, although horizontal space is used to indicate which statements are part of the loop, vertical space is not meaningful. Use blank lines in your code to make it easier to read.

1.2. while

The for loop is best when you have a list of objects and you want to do something with each one of them.

Other times, you will want to continue looping until a particular condition is met. Like this:

angle = 0
per = 6

while angle < 360:
forward(10)
right(per)
angle += per
• getACL = 0.539s
• init = 0.023s
• load_multi_cfg = 0.003s
• run = 12.844s
• send_page = 12.403s
• send_page_content = 3.308s
• total = 12.870s