Po Chun, Lu
    • 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
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Python - 常見錯誤 ###### tags: `Coding-X` ![](https://i.imgur.com/y0t5uE4.png) 程式錯誤訊息分成兩個部分: - 發生問題的位置(哪個檔案的第幾行) - 問題的原因(什麼錯誤) #### example ![](https://i.imgur.com/0pP9kTs.png) ``` File "test.py", line 4 <-- 哪個檔案的第幾行 print('fwfe) ^ <--- 第幾行的哪個地方有錯 SyntaxError: EOL.... <--- 錯誤種類跟錯誤原因 EOL.... <- 這個原因八成是括號有問題 ``` --- ## 1 SyntaxError 詞法錯誤:通常是格式錯誤 -> 打錯,拼錯了 就和你背英文單字背錯一樣 若錯誤訊息說有問題的那一行你看起來是沒問題的,那可以看他的前一行有沒有問題 (見 example 4) #### example 1.1 ```python3= speed = 10 iff speed == 10: print('OK') # File "./prog.py", line 2 # iff speed == 10: # ^ # SyntaxError: invalid syntax ``` 請檢查程式碼有沒有打錯 `iff` => `if` #### example 1.2 ![](https://i.imgur.com/bPRWKcN.png) ![](https://i.imgur.com/5ABqTkI.png) 可以看 ^ : 打成 ; 了 #### example 1.3 ![](https://i.imgur.com/z7uaGGl.png) ![](https://i.imgur.com/pDZeOjX.png) 看到 unexpcted EOF while parsing -> 大多數是括號有問題,括號括一半 #### example 1.4 ![](https://i.imgur.com/QnaOphU.png) ![](https://i.imgur.com/ZtLiU0p.png) 由於前面的程式碼少打或是打錯,導致python讀不懂下一行,所以錯誤訊息標示在這裡,請檢查前後是否有誤 ## 2 TypeError 型別錯誤,通常是把不能加的東西加在一起 #### example 2.1 ![](https://i.imgur.com/pkGkgE0.png) ![](https://i.imgur.com/NcjSTvE.png) 型別錯誤,`str` 跟`int`不能相加所以會跳錯 ## 3 NameError 變數錯誤,通常是變數名稱拼錯,或忘記先定義 #### example 3.1 ![](https://i.imgur.com/zxw9RbK.png) ![](https://i.imgur.com/61MRupR.png) 這樣寫意思是把 hello 這個變數印出來,但因為 hello 沒有定義(defined)所以會跳 Name Error,如果是要印出文字 hello 要記得加上 '' 如 `print('hello')` ## 4 ValueError #### example 4.1 ```python3= speed = 'a23' print(int(speed)) # Traceback (most recent call last): # File "./prog.py", line 2, in <module> # print(int(speed)) # ValueError: invalid literal for int() with base 10: 'a34' ``` ValueError: 表示提供的值是錯誤的 請確認值是否正確 本段的範例是,<code>int()</code>解析不了'a' ## 5. IndentationError 縮排不對,多按或少按一個 "空白鍵" 或 "tab按鍵", #### example 5.1 ![](https://i.imgur.com/w9l2i2q.png) ![](https://i.imgur.com/yYMo7wV.png) unexpected indent 表示縮排有問題,上面寫第二行縮排有問題,可以看到他多了一個空格 ## 6. TabError 原則上 鍵盤左上角的 "tab 按鍵" 可以設定成輸出 "空白字元" 或是 "tab字元", python 建議將 "tab 按鍵" 統一設定輸出"空白字元"(4個空白) 通常clou9 跟 vscode 都預設按下 tab 是輸出 4個空白,但如果動到設定可能就會跳出以下錯誤 #### example 6.1 ![](https://i.imgur.com/QFCfriJ.png) ![](https://i.imgur.com/kG6EHEg.png) 不小心混用了 '空白字元' 跟 'tab字元' 上方這個範例前面是 一個tab鍵 下方這個範例前面是 4個空白鍵 請調整右下角的縮排設定: 如 空格: 4 或是將有問題的縮排重新打 ![](https://i.imgur.com/7GujJlG.png) ## final ![](https://i.imgur.com/GryaneB.png)

    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