Goal: 這節課完成後將學會如何使用While Loop 完成老鼠走迷宮的程式
Built-in Functions - Python 3.10.1 documentation
Defining Functions
def my_function()
#Do this
#Then do this
#Finally do this
Calling Functions
my_function()
#挑戰遊戲一
def turn_right():
turn_left()
turn_left()
turn_left()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_right()
move()
#挑戰遊戲二
def turn_right():
turn_left()
turn_left()
turn_left()
for i in range(1,7):
move()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
#挑戰遊戲二
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
move()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
#While Loop與邏輯判斷
while at_goal() != True:
# 也可以寫成這樣 while not at_goal():
Identation
def my_function():
#以下為函數的程式碼區塊必須向右移動4個空格縮排,以此類推, 為程式碼區塊
****#Do this
#Then do this
Finally do this
https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Hurdle 3&url=worlds%2Ftutorial_en%2Fhurdle3.json
#挑戰遊戲三
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
#關鍵: 1. 要越過障礙牆的動作, 左,進,右,進,右,進,左
turn_left()
go_move()
turn_right()
go_move()
turn_right()
go_move()
turn_left()
def go_move():
#關鍵: 2. 要能自動判斷前方是否有牆
if front_is_clear():
move()
while at_goal() != True:
if wall_in_front():
jump()
else:
go_move()
#挑戰遊戲四
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()
#挑戰遊戲五
def turn_right():
turn_left()
turn_left()
turn_left()
def go_move():
if front_is_clear():
move()
while at_goal() != True:
#假設機器人一開始都是靠右邊走
if right_is_clear():
turn_right()
move()
#右邊有牆,就往前
elif front_is_clear():
move()
#若都不是, 就往左
else:
turn_left()
Note: 這裡還有老師的三個很難的作業