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

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

1. StartProgramming-1-7 列表Lists

In the loops in previous examples, we used code like this:

在前面例子的循环中,我们这样写代码:

for x in 1, 2, 3, 4:

The series of numbers 1, 2, 3, 4 is a called a sequence, and loops can iterate over the members of any sequence.

这一串数 1, 2, 3, 4 叫做 序列,并且循环会 列举 任何序列中的所有成员。

What this code means is: "set x = 1 and do the following things, then set x = 2, and do these things again", etc, until the end of the sequence.

这些代码的意思:“设置 x = 1 并且做下面的事情,然后设置x = 2,并且再次作这些事情”等等,直到序列结束。

A sequence like this will often be written either in square brackets [1, 2, 3, 4] (called a list) or in parentheses (1, 2, 3, 4) (a tuple).

像这样的序列常常被放在中括号里 [1, 2, 3, 4] (叫做一个 列表) 或者在圆括号里 (1, 2, 3, 4) (一个元组)。

The difference between these is a bit subtle.

两者之间有着细小的差别。

A list can be changed after it is constructed (we say that it is mutable), whereas a tuple cannot be changed (immutable).

一个列表创建之后可以被改变(我们说它是 不定的),然而一个元组不能被改变 (固定的)。

The differences will become more clear later, but I will use the word "list" for now to keep things simpler.

这些不同稍后会变得更加清楚,但是我现在先简单的使用单词“list列表”。

1.1. 创建Create

It is very common to give the list a name instead of typing the list itself in to a loop statement. When creating a list like this, brackets are required.

在循环中使用列表最普遍的做法是用一个列表名字代替键入列表本身。像这样创建一个列表,必须要有方括号。

You can make an empty list:

你可以建立一个空列表:

empty_list = []

or if you know exactly what needs to be in the list, just go ahead and fill it up:

或者你完全知道列表中需要什么,接着把它们填充进去:

sides = ['side one', 'side two', 'side three', 'side four']

Sometimes, especially if the elements in the list are long, it looks better to break it up in to multiple lines. As long as you are inside the brackets, that is fine:

有时,列表中的元素比较长,放到多行中看起来会好一些。只要你把它们都放在中括号里就可以了:

   1 verbose_sides = [
   2   'The side known as "side one"',
   3   'This one is side two',
   4   'Another side, called "side three"',
   5   'The final side, or side four'
   6 ]

1.2. 改变Mutate

Lists are mutable, but what exactly does that mean?

列表是可变的,但是如何正确的改变呢?

Lists are objects with special methods built in to make it easy to add, remove, sort, and find things that are in the list.

列表是一个拥有特殊内置方法的对象,使得它可以简单的添加,删除,排序和查找列表的内容。

For instance, if you did not know beforehand what words you wanted to print around the square, you could add them to the list as you found out:

例如,如果你不知道在正方形周围先打印什么单词,当你弄清楚时你可以把它们添加到列表中:

   1 from pygsear.Widget import Dialog_LineInput
   2 sides = []
   3 for side in 'first', 'second', 'third', 'fourth':
   4     message = 'Enter the name of the %s side' % side
   5     namegetter = Dialog_LineInput(message=message)
   6     name = namegetter.modal()
   7     sides.append(name)

1.3. 循环Iterate

In this case, the members in the list sides are not numbers, but text (called strings), so we could not use them in math formulas, but we could use them like this:

在这个情况中,列表sides 的成员并不是数字,而是文字(叫做 字符串),所以我们不能把它们用在数学公式里,但是我们可以这样用它们:

   1 for word in sides:
   2     write(word)
   3     forward(180)
   4     right(90)

Or, more generally:

或者,更通用的:

   1 def messageSquare(messages=['one', 'two', 'three', 'four']):
   2     for word in messages:
   3         write(word)
   4         forward(180)
   5         right(90)

The function messageSquare() takes one parameter, and if called without a value it will use the default argument ['one', 'two', 'three', 'four'].

函数messageSquare() 需要一个参数,并且如果被调用时参数没有值的话,它将会用 缺省值 ['one', 'two', 'three', 'four'].

In other words, a call to:

换句话说,调用:

messageSquare()

is the same as if you called it:

等同于你调用:

messageSquare(['one', 'two', 'three', 'four'])

You can also call it with a totally different list:

你也能够用一个完全不同的列表调用它:

messageSquare(['penguins', 'like', 'to eat', 'herring'])

or with a list created before:

或者用一个之前创建的列表:

messageSquare(sides)

1.4. 排列Range

The function messageSquare() has a problem similar to the problem with the function at the end of the last page. If you pass in a sequence which is longer or shorter than 4 elements, it is probably not going to do what you want.

函数messageSquare()有一个问题,相似问题的函数在上一页的结尾部分。如果你换一个多于或者少于4个元素的列表,它很可能不会按照你的要求来做。

Try it now. If the sequence is less than 4 elements, it will not make a complete square. If the sequence is more than 4 elements, it will write over the words on previous sides.

现在就试一下。如果序列少于4个元素,它就不能完成一个四边形。如果序列多于4个元素,它将覆盖前面写的单词。

   1 reset()
   2 messageSquare(['one', 'side', 'short'])
   3 reset()
   4 messageSquare(['one', 'side', 'too', 'many', 'here'])

To get around this problem, we need to figure out what angle to turn from the number of sides we want the shape to have.

要避开这个问题,我们需要算出我们希望的多边形需要转多少度的角。

Using the built-in function range() we can make a very general function to draw polygons:

用内置函数 range()我们可以做一个非常通用的函数来画多边形:

   1 def poly(sides, length):
   2     angle = 360.0 / sides
   3     for side in range(sides):
   4         forward(length)
   5         right(angle)

The function range() takes a number, and returns a list with that many elements.

函数range()需要一个数字,并且返回一个有许多元素的列表。

That means: if sides=5 then range(sides) is the same as range(5) and it returns [0, 1, 2, 3, 4]

意思是:如果sides=5那么range(sides)等同于range(5)并返回[0, 1, 2, 3, 4]

So the function call poly(5, 10) will draw a five-sided figure with sides of length 10.

所以调用函数poly(5, 10)将画一个边长是10的五边形。

Now we can make almost any sort of regular polygon by giving poly() the number of sides and the length of each side:

现在我们可以通过给出poly()边数和边长来画出几乎任意种类的正多边形:

   1 poly(5, 20)
   2 poly(6, 30)
   3 poly(100, 3)

The last one will probably be indistinguishable from a circle. In fact, if pete did not have a built in circle() method, that would be a good way to simulate a circle.

最后一个很可能无法和一个圆区分开。事实上,如果pete没有内置的函数circle(),这是也许个代替画圆的好办法。