Hurdle 4 - Challenge while loop


wall_in_front( ) 與 front_is_clear(),是一樣的意思,後者是相反的說法。 wall_on_right( ) 與 right_is_clear(),是一樣的意思,後者是相反的說法。

Coding - Student Laurence


我的寫法,雖然可以達到終點。 但在某些條件下,機器人的臉無法朝右,只會朝下。

def turn_right():
    turn_left()
    turn_left()
    turn_left()
        
while not at_goal():
    if wall_in_front() and wall_on_right():
        turn_left()
    elif front_is_clear() and wall_on_right():
        move()
    elif front_is_clear() and right_is_clear():
        turn_right()
        move()
    elif wall_in_front() and right_is_clear():
        turn_right()
        move()        
    else:
        move()

Coding - Teacher Angela


def turn_right():
    turn_left()
    turn_left()
    turn_left()

def jump():
    turn_left()
    while wall_on_right():
        move()
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()
        
while not at_goal():
    if wall_in_front():
        jump()
    else:
        move()