練習33:while循環

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章