《笨办法学python》加分习题33——我的答案
2017-09-03 22:32
387 查看
这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!
**
**
1、
2、
3、
4、一样的
5、
运行结果:
![](https://img-blog.csdn.net/20170903232126959?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWXhXYW5nMTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
一开始我的for-loop是这样写的:
然后就一直报错:
说是int不能用来迭代,我就去翻看前面的练习和和上网找资料,最后使用的是将bout通过range改为list。
在此感谢:惹不起的程咬金,我在这里得到解释和答案。
**
原文例题:
**i = 0 numbers =[] 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" % i print "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 ex33 ex33.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: ", numbers loop(5)
运行结果:
一开始我的for-loop是这样写的:
for number in bout:
然后就一直报错:
int' object is not iterable
说是int不能用来迭代,我就去翻看前面的练习和和上网找资料,最后使用的是将bout通过range改为list。
在此感谢:惹不起的程咬金,我在这里得到解释和答案。
相关文章推荐
- 《笨办法学python》加分习题19——我的答案
- 《笨办法学python》加分习题26——我的答案
- 4000 《笨办法学python》加分习题13——我的答案
- 《笨办法学python》加分习题14——我的答案
- 《笨办法学python》加分习题15——我的答案
- 《笨办法学python》加分习题20——我的答案
- 《笨办法学python》加分习题25——我的答案
- 《笨办法学python》加分习题18——我的答案
- 《笨办法学python》加分习题41——我的答案
- 《笨办法学python》加分习题16——我的答案
- 《笨办法学python》加分习题28——我的答案
- 《笨办法学python》加分习题29——我的答案
- 《笨办法学python》加分习题36——我的答案
- 《笨办法学python》加分习题42——我的答案
- 《笨办法学python》加分习题4——我的答案
- 《笨办法学python》加分习题35——我的答案
- 《笨办法学python》加分习题5——我的答案
- 《笨办法学python》加分习题7——我的答案
- 《笨办法学python》加分习题8——我的答案
- 《笨办法学python》加分习题9——我的答案