Yuxin Zhou
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Spyder Debugger Tutorial === ## 前言 通常我們寫程式會有兩種 Debug 的方法: 1. 直接在程式裡面加上一些 print 函式,就可以知道在特定位置時的變數數值有沒有不符合想像的,再回去修正程式。 2. 使用 IDE 附帶的 Debugger,不必額外加入 print 函式,而是使用 IDE 附帶的 Varible explorer 面板來看各個變數執行到特定位置時的值(透過逐行執行程式),從中發現錯誤再回去修改程式。 這篇文章主要會介紹第 2 種,如何使用 IDE 附帶的 Debugger 來為你的程式除錯。我們使用的 IDE 是 Spyder3。 ## Debugger 操作介面介紹 ![Debugger 操作介面介紹](https://i.imgur.com/UGrFVLZ.png) 上面這張圖是 Sypder 打開時可能長的樣子。有幾個區塊要特別注意: 1. 右邊的 **Variable explorer**,記得將右上方的視窗切換成 Variable explorer,這樣才能知道程式執行到特定位置時,變數的值分別為何。 2. 上方工具列中央的 **藍色按鈕群**,這些是待會兒會介紹到的 Debugger 操作部分。 3. 左方的 **程式碼區塊**。在操作 Debugger 時,請仔細觀察編輯器中到底執行到哪一行。執行到的那一行背景會有顏色。 ## Debugger 的概念 通常我們需要 Debug 的時候都是程式最後跑出來的結果不符合我們的預期,或是程式跑到一半出現問題。 所以,Debugger 提供我們以下功能: 1. **能夠逐行執行程式**:因為程式中途不知道哪裡寫錯了,我們需要知道程式執行到每一行時,變數的值是什麼。如此才能知道從哪裡開始有錯誤。 2. 進階一點,**可以設定中斷點,每次執行就一次執行到下個中斷點**:我們可能覺得只有一部分的程式碼會出錯,那其他部分就不需要逐行執行;於是 Debugger 也賦予我們能夠設定「中斷點」,每次執行就直接一路執行到下一個中斷點。 3. 在每次執行後,**可以查看當前所有變數的值**:讓我們可以觀察程式到該行時,變數的大小、正負號、內容是否合理,並從中發現錯誤。(這裡的執行可以是逐行執行,執行了一行之後。或是執行到下一個中斷點後。) 4. 能夠知道 **目前執行到哪一行了**:如下圖,有紅色背景那行即為目前執行到的地方。 ![](https://i.imgur.com/8evrsgF.png) 接著介紹在 Spyder 中如何操作這些功能。 ## Spyder 中的 Debugger 按鈕 ![](https://i.imgur.com/FQtiHIH.png) 從左到右的按鈕意義分別為: 1. **Debug file**(開始 Debug 程式碼):按下這個按鈕後,就可以開始對目前開的程式進行 Debug。 2. **Run current line**(執行這一行):如上段提到的**逐行執行程式**,按一下該按鈕就會直接當前這一行。 3. **Step into function or method of current line**(進入 function debug):假設我有以下的程式碼: ```python3= def add(a, b): sum = a + b return sum x = 5 y = 3 z = add(x, y) print(x, '+', y, '=', z) ``` 並假設目前 Debugger 執行到 `z = add(x, y)` 這行。此時如果按下 **Run current line**,程式會直接跳到下一行 `print` 的部分;但如果按 **Step into function...** 這個按鈕,則會進到 `def add(a, b):` 的函式中,可以繼續逐行執行函式內的每一行。 4. **Run until current function or method returns**(直接執行完函式):承接上項,如果我在函式內逐步執行,後來覺得這個函式一定沒有問題,那就可以按這個按鈕,直接一次幫你執行到 return 的地方,不需要一直逐行執行。 5. **Continue execution until next breakpoint**(執行至下一個中斷點):如前段提到的**執行到下一個中斷點**,假設你的程式已經設定好中斷點是在哪幾行,那麼只要按這個按鈕,Debugger 就會自動一路執行到下一個中斷點之處。 ## 如何設定中斷點(breakpoint) ![](https://i.imgur.com/qdom5Yg.png) 在上圖中,有三個中斷點(breakpoint),分別在 **第 19 行**、**第 23 行** 與 **第 26 行**。而目前執行到 **第 19 行**。 中斷點設定方式:在該行的數字上,**雙擊滑鼠左鍵**,就會出現紅色點點。取消中斷點的方式一樣。 ### 說明 假設依照上圖,目前執行到 **第 19 行**,那麼如果我按下 1. **Run current line** 則應該會執行到第 20 行。 2. **Continue execution until next breakpoint** 則會一路執行到第 23 行。 此時,你可以在 Variable explorer 看到變數的值: ![](https://i.imgur.com/UAdAVYH.png) 你可以自己親手用圖中的範例程式碼來測試: ```python3= # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ def calc_add(a, b): sum = a + b return sum def calc_sum(a, b): sum = 0 for i in range(a, b + 1): sum = sum + i return sum x = 1 y = 100 z = 10 #nonsense, just for debugger tutorial a = 1 #nonsense, just for debugger tutorial b = 2 #nonsense, just for debugger tutorial c = 3 #nonsense, just for debugger tutorial print(calc_add(x, y)) print(calc_sum(x, y)) ``` ## 參考資料 [Youtube - Spyder debug](https://youtu.be/9d0Oi2W33FQ)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully