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
这个程序并不难,需要知道的就是第一块里面的 append(i) 是为了给 numbers 加数字用的
第二节的 for 更不用说了,就是输出 numbers 里面的数字而已
运行结果如下:
=====================================================================================
附加练习:
1-4 参考
i = 0
print "Input a for 'while i < a': "
a = int(raw_input("> "))
print "Input b for 'i = i + b': "
b = int(raw_input("> "))
numbers = []
while i < a:
print "At the top i is %d" % i
numbers.append(i)
i = i + b
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
运行结果
5.
i = 0
print "Input a for 'i in range(0,a)': "
a = int(raw_input("> "))
#print "Input b for 'i = i + b': "
#b = int(raw_input("> "))
numbers = []
for i in range(0,a):
print "At the top i is %d" % i
numbers.append(i)
# i = i + b
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
运行结果:
现在试一试去掉注释中的 i = i + b
结果就不贴了,好像没什么区别