i = 0
numbers= []
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
运行该程序:
$python ex33.py
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
$
加分习题:
1.将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。
ex33_1.py
def while_list(number):
i = 0
numbers = []
while i < number:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
调用该函数试试看:
>>> import ex33_1
>>> ex33_1.while_list(1)
At the top i is 0
Numbers now: [0]
At the bottom i is 1
>>> ex33_1.while_list(2)
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
>>> ex33_1.while_list(6)
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
>>>
2.使用这个Python 函数重写你的脚本,并用不同的数字进行测试。
ex33_2.py
def while_list(number):
i = 0
numbers = []
while i < number:
print "At the top i is %d" % i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
nums = int(raw_input("Pls input an int> "))
while_list(nums)
3.为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以让它任意加值了。
ex33_3.py
def while_lists(number, adds):
i = 0
numbers = []
while i < number:
print "At the top i is %d" % i
numbers.append(i)
i = i + adds
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
调用该函数试试看:
>>> import ex33_3
>>> ex33_3.while_lists(10, 2)
At the top i is 0
Numbers now: [0]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 2]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 2, 4]
At the bottom i is 6
At the top i is 6
Numbers now: [0, 2, 4, 6]
At the bottom i is 8
At the top i is 8
Numbers now: [0, 2, 4, 6, 8]
At the bottom i is 10
>>>
4.再使用该函数重写一遍这个脚本。看看效果如何。
ex33_4.py
def while_lists(number, adds):
i = 0
numbers = []
while i < number:
print "At the top i is %d" % i
numbers.append(i)
i = i + adds
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
while_lists(20, 3)
运行该程序:
$python ex33_4.py
At the top i is 0
Numbers now: [0]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 3]
At the bottom i is 6
At the top i is 6
Numbers now: [0, 3, 6]
At the bottom i is 9
At the top i is 9
Numbers now: [0, 3, 6, 9]
At the bottom i is 12
At the top i is 12
Numbers now: [0, 3, 6, 9, 12]
At the bottom i is 15
At the top i is 15
Numbers now: [0, 3, 6, 9, 12, 15]
At the bottom i is 18
At the top i is 18
Numbers now: [0, 3, 6, 9, 12, 15, 18]
At the bottom i is 21
The numbers:
0
3
6
9
12
15
18
$
5.接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?
ex33_5.py
numbers = []
for i in range(6):
print "At the top i is %d" % i
numbers.append(i)
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
运行该Python 程序:
$python ex33_5.py
At the top i is 0
Numbers now: [0]
At the bottom i is 0
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 1
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 2
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 3
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 4
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 5
The numbers:
0
1
2
3
4
5
$
6.很有可能你会碰到程序跑着停不下来了,这时你只要按着 CTRL 再敲 c (CTRL-c),这样程序就会中断下来了。
至此,笨方法学Python-ex33-While 循环这一小节的学习结束了。