原稿示例代码:
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、2:将这个while循环改成一个函数,将测试条件(i<6)中的6换成一个变量,使用该函数重写你的脚本
#-*- coding:UTF-8 -*-
j = int(raw_input('>')) #定义一个新的参数,并且该参数从控制台输入值,由str强转为int
def loop(j): #定义一个循环函数,传参j
i = 0 #创建一个新的参数,并赋值为0
numbers = [] #创建一个空的列表
while i < j: #使用while
print "At the top i is %d" % i #打印i值
numbers.append(i)