Differences between revisions 1 and 2
Revision 1 as of 2006-12-29 02:55:52
Size: 6509
Editor: thinker
Revision 2 as of 2009-12-25 07:13:54
Size: 6518
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
[:StartProgramming: (首页)开始编程之旅]
翻译自Lee Harr的[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html Start Programming]
[[StartProgramming| (首页)开始编程之旅]]
翻译自Lee Harr的[[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html|Start Programming]]
Line 7: Line 7:
本文是使用[http://www.nongnu.org/pygsear/ pygsear]+[http://pygame.org pygame]作为开发环境,以初级用户角度来分步分阶段学习[http://www.python.org PYTHON]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。 本文是使用[[http://www.nongnu.org/pygsear/|pygsear]]+[[http://pygame.org|pygame]]作为开发环境,以初级用户角度来分步分阶段学习[[http://www.python.org|PYTHON]]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。
Line 9: Line 9:
[[TableOfContents]] <<TableOfContents>>

(首页)开始编程之旅 翻译自Lee Harr的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:

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

   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.


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.


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.


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)
   8 side = 2
   9 forward(100)
  10 right(90)
  12 side = 3
  13 forward(100)
  14 right(90)
  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
   4 while angle < 360:
   5     forward(10)
   6     right(per)
   7     angle += per        

StartProgramming-1-5 (last edited 2009-12-25 07:13:54 by localhost)