《笨办法学python》加分习题33——我的答案

来源:互联网 发布:淘宝卖家注册账号申请 编辑:程序博客网 时间:2024/05/22 14:40

这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!

**

原文例题:

**

i = 0numbers =[]while i < 6:    print "At the top i is %d" % i    numbers.append(i)    i += 1    print "Numbers now: ", numbers    print "At the bottom i is %d" % iprint "The numbers: "for num in numbers:    print num

**

习题答案:

**
1、

def loop(bout):    i = 0    numbers =[]    while i < bout:        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

2、

import ex33ex33.loop(6)print "\n"ex33.loop(4) 

3、

def loop(bout):    i = 0    numbers =[]    while i < bout:        print "At the top i is %d" % i        numbers.append(i)        temp = i + 1        print "Numbers now: ", numbers        print "At the bottom i is %d" % temp    print "The numbers: "    for num in numbers:        print num

4、一样的
5、

def loop(bout):    numbers = []    for number in range(0, bout):        numbers.append(number)        print "Numbers now: ", numbersloop(5)

运行结果:
这里写图片描述
一开始我的for-loop是这样写的:

for number in bout:

然后就一直报错:int' object is not iterable
说是int不能用来迭代,我就去翻看前面的练习和和上网找资料,最后使用的是将bout通过range改为list。
在此感谢:惹不起的程咬金,我在这里得到解释和答案。

阅读全文
1 0
原创粉丝点击