while 迴圈 while loop


for loop 與 while loop 的差異

for item in list_of items:
    #Do something to each item

for number in range(a, b):
    print(number)
while something_is_true:
    #Do something repeatedly

Hurdle 1 - for example while loop


Line 19 在 while loop 的程式碼區塊的最後一行,每次執行到這邊,就遞減1。 目的是為了,最後讓 while loop 停下來,當 number_of_hundles = 0 時 。

**Line 20 觀察,**每次遞減1後的結果。

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def jump():
    move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

number_of_hundles = 6
while number_of_hundles > 0:
    jump()
    number_of_hundles -= 1
    print(number_of_hundles)

Hurdle 2 - Challenge while loop


Line 16 在 while loop 的判斷條件,為 at_goal( ) 不等於 True。 at_goal( ),代表是終點(有旗子)。

所以,只要那一步(x, y)不是終點的話,while loop 就會持續執行下去。

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def jump():
    move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

while at_goal() != True:
    jump()

Line 16 在 while loop 的判斷條件,為 not at_goal( )。 at_goal( ),代表是終點(有旗子)。