while循環
格式:
while <布爾表達式> :
語句
while循環會循環運行語句內的代碼段直到布爾表達式的值爲False
練習代碼
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
改寫爲函數並用for循環替代while循環
def number(number_max):
numbers = []
for i in range(0,number_max) :
print "At the top i is %d" % i
numbers.append(i)
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num