Loops <<
Previous Next >> SA
Recursion
遞迴(Recursion)
用函式自己呼叫自己來處理重複任務。
# 範例 1:走到底(遞迴版)
def walk():
if not wall_in_front():
move()
walk()
walk()
# 範例 2:撿起所有物品(遞迴版)
def pick_all():
if object_here():
take()
pick_all()
pick_all()
# 範例 3:探索迷宮(簡化遞迴)
def explore():
if not wall_in_front():
move()
explore()
else:
turn_left()
explore()
explore()
Loops <<
Previous Next >> SA