cp2025 計算機程式

  • Home
    • SMap
    • reveal
    • blog
  • About
    • cs101
    • Computer
      • llama
      • nginx
    • AI
      • QandA
      • Teams
    • Homework
  • Topics
    • Git
      • Git_ex1
      • Git_ex2
    • Python
      • SE
      • CL
      • Loops
      • Recursion
      • SA
      • SST
      • Maze
      • Collect
      • GT
      • Python_ex1
    • Javascript
      • HTML and CSS
    • Project
      • Waitress
      • API
  • Brython
    • Brython_ex
    • Robot_ex
  • Ref
    • Reeborg
      • ex1
      • Otto_ninja
    • tkinter
    • Pyodide
    • Pyodide_ex
    • Pyodide2
      • robot.py
      • Example2
    • Pyodide3
      • png_files
      • Harvest
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

Copyright © All rights reserved | This template is made with by Colorlib