(首页)开始编程之旅 翻译自Lee Harr的Start Programming

本文是使用pygsear+pygame作为开发环境,以初级用户角度来分步分阶段学习PYTHON基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。

1. StartProgramming-1-5 循环Loops

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

我们重复用相同的两步 -- 正方形的每个边来画一个正方形:

   1 forward(100)
   2 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:

这是我们通知 pete 如何用循环来画一个正方形:

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

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

到行的开头输入第一行语句 for side in 1, 2, 3, 4: 并按 [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?"

这里的意思是在开始执行前等待更多的输入。你在说 "我想把一些事情做4次",电脑回答说 "好的,你想做什么事情4次?"

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

在Python里,缩进可以告诉计算机哪些是要循环的部分。

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]

在每一层缩进上用4个空格。到行的开头并键入4个空格,并且在第二行上输入要循环的语句 forward(100) 并按 [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.

它不知道你是已经完成了4次循环的内容,还是在循环里面有更多的步骤。

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

当然是循环里还有其他的步骤,键入4个空格,然后在最后一行输入 right(90) 并按 [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.

要结束循环,再次按下 [ENTER]。

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...

这个循环比较的简单,我们可以把它展开并看看究竟发生了什么事情……

   1 for side in 1, 2, 3, 4:
   2     forward(100)
   3     right(90)
   4         side = 1
   5 forward(100)
   6 right(90)
   7 
   8 side = 2
   9 forward(100)
  10 right(90)
  11 
  12 side = 3
  13 forward(100)
  14 right(90)
  15 
  16 side = 4
  17 forward(100)
  18 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.

首先,变量 side 从来没有在循环里被用上。你可以为它起一个不同的名字。并且不会有什么差别。其他合理的名字可以是 count 或者 i 或 x。 尽管这样你仍然要小心一点,如果用 forward 为变量命名会发生错误。

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.

当你有一个对象列表,并且你想让列表中的每一个对象都做一些事情, for 循环是一个非常好的选择。

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

其他时候,你想继续一个循环,除非一个特殊的情况发生。像这样:

   1 angle = 0
   2 per = 6
   3 
   4 while angle < 360:
   5     forward(10)
   6     right(per)
   7     angle += per