1.将这个while 循环改成一个函数,将测试条件(i < 6)中的6换成一个变量。
2.使用这个函数重写你的脚本,并用不同的数进行测试
def list(i):
numbers = [ ]
j = 0
while j < i:
print "At the top number is %d" % j
numbers.append(j)
j = j + 1
print "Number now: ", numbers
print "At the bottom number is %d" % j
print "The numbers: "
for num in numbers:
print num
print "numbers: "
i = int(raw_input(">"))
list(i)
3.为函数添加另外一个参数,这个参数用来定义第8行的+1,这样你就可以让它任意递增了。
4.再使用该函数重写一遍这个脚本,看看效果如何
def list(i, z):
numbers = [ ]
j = 0
while j < i:
print "At the top number is %d" % j
numbers.append(j)
j = j + z
print "Number now: ", numbers
print "At the bottom number is %d" % j
print "The numbers: "
for num in numbers:
print num
print "number: "
i = int(raw_input(">"))
print "number: "
z = int(raw_input(">"))
list(i, z)
5.接下来使用for 循环和range把这个脚本再写一遍。还需要中间的递增操作吗?如果不去掉它,会有什么样的结果?
def list(i):
numbers = [ ]
for j in range(0, i):
print "At the top number is %d" % j
numbers.append(j)
print "Numbers now: ", numbers
print "At the bottom number is %d" % j
print "The number: "
for num in numbers:
print num
print "number: "
i = int(raw_input(">"))
list(i)