《笨方法学Python》习题33

习题 33: While 循环
在文本编辑器中,编辑以下内容并保存到ex33.py文件中,同时在终端中运行该文件:

#coding:utf-8
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 is:"

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 is:
0
1
2
3
4
5

加分习题

  1. 将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。
#coding:utf-8
i = 0
numbers = []

x = int(raw_input("please input the number:")) # 注意要使用int类型转换,不然会导致程序运行停不下来

while i < x:
    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 is:"

for num in numbers:
    print num

执行结果:

$ python ex33.py
please input the number:4
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
The numbers is:
0
1
2
3
  1. 使用这个函数重写你的脚本,并用不同的数字进行测试。

  2. 为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以 让它任意加值了。

i = 0
numbers = []

x = int(raw_input("please input the end number:"))
y = int(raw_input("please input the step:"))

while i < x:
   print "At the top i is %d" % i
   numbers.append(i)
   i = i + y   #加参数y
   print "numbers now:", numbers
   print "At the bottom i is %d" % i

print "The numbers is:"

for num in numbers:
   print num

执行结果:

$ python ex33.py
please input the end number:14
please input the step: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
At the top i is 10
numbers now: [0, 2, 4, 6, 8, 10]
At the bottom i is 12
At the top i is 12
numbers now: [0, 2, 4, 6, 8, 10, 12]
At the bottom i is 14
The numbers is:
0
2
4
6
8
10
12
  1. 再使用该函数重写一遍这个脚本。看看效果如何。
  2. 接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?
#coding:utf-8
i = 0
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 is:"

for num in numbers:
    print num

执行结果:

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 is:
0
1
2
3
4
5

注意:很有可能你会碰到程序跑着停不下来了,这时你只要按着 CTRL 再敲 c (CTRL-c), 这样程序就会中断下来了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容