Ocean_1029
    • 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
    • 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 Note Insights 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

    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 程式小測驗 ## 程式 1. 要在 Terminal 印出一段話要怎麼做 ```python= print("Hello, World!") ```` 1. **變數的作用是什麼?** - A. 儲存資料 - B. 執行迴圈 - C. 判斷條件 - D. 輸出結果 ```python= a = 3 # name = a, value = 3 type = int type(a) - integer # 整數 - string # 字串 / 文字 - float # 小數 ``` ``` python a = input("輸出的東西") blablabla() -> function 函式 # 意思相等於 print("輸出的東西") a = input() ``` 2. **以下哪一行程式碼會正確地接收使用者的輸入並儲存到變數 `age` 中?** - A. `input = "age"` - B. `age = input("請輸入你的年齡:")` - C. `print("age")` - D. `age = print("請輸入你的年齡:")` 3. 接續第二題, age 被成功儲存後,預設的資料型態是什麼 - A. String / 字串 - B. Interger / 整數 - C. float / 浮點數 4. 接續第二題,如果我要把變數以「整數 / interger」的方式儲,我該怎麼做? ``` python # 請在這裡回答 / Please answer here Chace= int(input("")) number = int("123") if number > 10: print("Haha") else: print("LOL") ``` 5. 布林值(Boolean Value) 只有兩種 Value,分別為 True or False,通常會用來進行條件判斷 (if-elif-else),來試試看下面的 Boolean Value 哪些是 True 吧 - A. 5>2 and 3>7 - B. 5>2 or 3>7 - C. (5>2) and (3<7 or 3==7) 5. 請完成以下程式碼,使其根據使用者輸入的分數判斷是否及格(分數 ≥ 60 為及格): ```python score = int(input("請輸入你的分數:")) if (score >= 60): print("good") else (score < 60):: print("Ha~") ``` 6. **向使用者問好 ~** ```python name = input("What is your name: ") print("Hello" + name) ``` 7. **寫一個程式,讓使用者輸入一個數字,然後判斷這個數字是正數、負數還是零,並印出相應的結果。** ```python int = input("Get me a integer:") if(int==0): print("zero") if(int>0): print("Positive Number") if(int<0): print("Negtive Number") else: print("Nothing") ``` **範例輸入/輸出:** ``` 請輸入一個數字:5 這是一個正數。 ``` 8. **寫一個簡單的程式,讓使用者輸入兩個數字,然後輸出它們的和。** **範例輸入/輸出:** ``` 請輸入第一個數字:3 請輸入第二個數字:4 兩數之和為:7 ``` ```python= num_1st = int(input("first number")) num_2nd = int(input("second number")) print(num_1st + num_2nd) ``` --- ## 電腦科學 1. 為什麼在台灣卻可以玩到伺服器在美國(可能)的荒野亂鬥? https://www.thenewslens.com/feature/war-communication/163125 2. 工程師是怎麼跟電腦溝通的? --- ## 實務問題 1. 單位換算:https://pdogs.ntu.im/problem-set/1/46/challenge/471/problem/1465 2. 金錢系統 - 找零 https://www.coursera.org/learn/pbc1/assignment-submission/RYnNM/di-san-zhou-zuo-ye-gai-zen-mo-zhao-qian-jin-jie-ban/attempt - 轉帳 https://www.coursera.org/learn/pbc1/assignment-submission/MPk9a/di-san-zhou-lian-xi-zhuan-zhang-jin-e-que-ren/attempt --- ## 專題嘗試 ### **專題名稱:餐廳點餐系統** ### **專題概述** 你將設計並實作一個簡單的餐廳點餐系統,使用者可以選擇不同的餐點類別(如主餐、飲料、甜點),再進一步選擇具體的餐點,最後顯示訂單明細和總金額。 ### **專題功能需求** 1. **主選單**: - 顯示餐點類別選項:主餐、飲料、甜點。 - 接收使用者選擇的類別編號。 2. **子選單**: - 根據使用者在主選單的選擇,顯示相應的餐點選項及價格。 - 接收使用者選擇的餐點編號。 3. **訂單明細**: - 根據使用者的選擇,顯示所選餐點的名稱和價格。 - 計算並顯示總金額。 4. **錯誤處理**: - 當使用者輸入無效的選項時,顯示錯誤訊息並提示重新選擇。 ### **範例運行結果** #### **範例 1:選擇主餐** ``` 歡迎光臨簡易餐廳點餐系統! 請選擇餐點類別: 1. 主餐 2. 飲料 3. 甜點 請輸入選項編號(1-3):1 主餐選單: 1. 牛排 - $250 2. 雞排 - $150 3. 義大利麵 - $200 請選擇主餐(1-3):2 您的訂單明細: 餐點:雞排 價格:$150 總金額:$150 ``` #### **範例 2:選擇飲料** ``` 歡迎光臨簡易餐廳點餐系統! 請選擇餐點類別: 1. 主餐 2. 飲料 3. 甜點 請輸入選項編號(1-3):2 飲料選單: 1. 可樂 - $50 2. 果汁 - $60 3. 水 - $30 請選擇飲料(1-3):1 您的訂單明細: 餐點:可樂 價格:$50 總金額:$50 ``` #### **範例 3:無效選擇** ``` 歡迎光臨簡易餐廳點餐系統! 請選擇餐點類別: 1. 主餐 2. 飲料 3. 甜點 請輸入選項編號(1-3):4 無效的餐點類別選擇。 沒有訂單。 ``` ### **專題步驟指引** 以下是完成這個專題的一些建議步驟,供你參考: 1. **設計主選單**: - 使用 `print()` 函數顯示餐點類別選項。 - 使用 `input()` 函數接收使用者的選擇。 2. **實作子選單邏輯**: - 根據主選單的選擇,使用 if-else 判斷來顯示相應的餐點選項。 - 在每個餐點類別內,使用巢狀的 if-else 來處理具體餐點的選擇。 3. **計算總金額**: - 根據使用者選擇的餐點,累加價格到總金額變數中。 4. **顯示訂單明細**: - 使用 `print()` 函數輸出所選餐點名稱和價格。 - 顯示總金額。 5. **處理無效選擇**: - 當使用者輸入的選項不在有效範圍內時,顯示錯誤訊息。 ### **擴展挑戰** 當你完成基本功能後,可以嘗試以下擴展挑戰,讓專案更加豐富: 1. **多項選擇功能**: - 允許使用者在每個類別中選擇多個餐點,並計算總金額。 2. **添加折扣系統**: - 根據總金額提供不同的折扣,例如總金額超過 $300 享有 10% 折扣。 4. **重複點餐功能**: - 使用 `while` 迴圈讓使用者可以反覆點餐,直到選擇退出。 --- # 11/8 for 迴圈 / while 迴圈 ## 課程目標 - 理解迴圈的基本概念及其在程式中的重要性 - 掌握 `for` 迴圈和 `while` 迴圈的基本語法和運作方式 - 能夠運用迴圈解決實際的程式問題 - 培養邏輯思維能力和問題解決能力 --- ## 課程大綱 ### 第一部分:迴圈的基本概念 #### 一、什麼是迴圈? 迴圈是一種讓程式碼重複執行多次的結構。 ```python= number = 3 while not STOP: write down an number on the paper number += 1 ``` ```python= # write_numbers.py # 定義要寫入的文件名 filename = 'numbers.txt' # 使用 with 語句打開文件,確保文件操作後自動關閉 with open(filename, 'w') as file: # 循環寫入 1 到 10 for number in range(1, 11): file.write(f"{number}\n") print(f"數字 1 到 10 已成功寫入 {filename}") ``` #### 二、迴圈的兩種類型 1. **`for` 迴圈**: - 適用於已知執行次數或遍歷序列(如列表、字串)的情況。 2. **`while` 迴圈**: - 適用於執行次數不確定,需依據條件來決定是否繼續的情況。 --- ### `while` 迴圈 #### 一、`while` 迴圈的基本語法 ```python while 條件: 程式碼區塊 ``` - **條件**:只要條件為真,迴圈就會繼續執行。 - **程式碼區塊**:每次迴圈要執行的程式碼。 #### 二、範例講解 1. **基本範例** ```python count = 0 while count < 5: print(count) count += 1 ``` - **解釋**:這段程式會輸出 0 到 4,因為當 `count` 小於 5 時,持續執行迴圈。 - **執行結果**: ``` 0 1 2 3 4 ``` 2. **使用者輸入** ```python while True: name = input("請輸入你的名字(輸入 'exit' 離開):") if name.lower() == 'exit': print("離開程式。") break print(f"你好,{name}!") ``` - **解釋**:這段程式會反覆要求使用者輸入名字,直到使用者輸入 'exit' 為止。 - **執行結果**: ``` 請輸入你的名字(輸入 'exit' 離開):小明 你好,小明! 請輸入你的名字(輸入 'exit' 離開):exit 離開程式。 ``` #### 三、應用練習 1. **練習題目一:計算使用者輸入的數字總和,直到使用者輸入 0 為止** ```python total = 0 while True: num = int(input("請輸入一個數字(輸入 0 結束):")) if num == 0: break total += num print("總和是:", total) ``` 2. **練習題目二:製作一個簡單的猜數字遊戲** ```python import random secret_number = random.randint(1, 10) guess = None while guess != secret_number: guess = int(input("猜一個 1 到 10 的數字:")) if guess < secret_number: print("太小了!") elif guess > secret_number: print("太大了!") else: print("恭喜你,猜對了!") ``` --- ### `for` 迴圈 #### 一、`for` 迴圈的基本語法 ```python for 變數 in 範圍: 程式碼區塊 ``` - **變數**:用來接收每次迴圈的值。 - **範圍**:可以是數字範圍(使用 `range()`)、列表、字串等。 - **程式碼區塊**:每次迴圈要執行的程式碼。 #### 二、範例講解 1. **使用 `range()` 進行迴圈** ```python for i in range(5): print(i) ``` - **解釋**:這段程式會輸出 0 到 4。 - **執行結果**: ``` 0 1 2 3 4 ``` 2. **遍歷字串** ```python word = "Python" for letter in word: print(letter) ``` - **解釋**:這段程式會依序輸出字串中的每一個字母。 - **執行結果**: ``` P y t h o n ``` #### 三、應用練習 1. **練習題目一:計算 1 到 10 的總和** ```python total = 0 for i in range(1, 11): total += i print("總和是:", total) ``` - **預期結果**:總和是:55 --- ### 第四部分:`for` 與 `while` 迴圈的比較 #### 一、適用情境 - **`for` 迴圈**: - 已知執行次數,例如遍歷列表、字串或使用 `range()` 進行固定次數的迴圈。 - **`while` 迴圈**: - 執行次數不確定,需要根據條件來決定是否繼續,例如等待使用者輸入、處理未知長度的數據等。 #### 二、優缺點 - **`for` 迴圈**: - **優點**:結構清晰,適合處理固定次數的迴圈,容易閱讀和維護。 - **缺點**:在需要根據條件動態調整迴圈次數時,較不靈活。 - **`while` 迴圈**: - **優點**:更靈活,可以根據條件動態調整迴圈次數。 - **缺點**:容易發生無限迴圈,需小心控制條件變化。 --- ### 第五部分:實作練習 #### 一、練習題目 1. **使用 `for` 迴圈列印 1 到 20 的奇數** ```python for i in range(1, 21): if i % 2 != 0: print(i) ``` - **預期結果**: ``` 1 3 5 7 9 11 13 15 17 19 ``` 2. 使用 for 迴圈,**製作一個九九乘法表** ```python for i in range(1, 10): for j in range(1, 10): print(f"{i} x {j} = {i*j}", end="\t") print() # 換行 ``` - **預期結果**: ``` 1 x 1 = 1 1 x 2 = 2 ... 1 x 9 = 9 2 x 1 = 2 2 x 2 = 4 ... 2 x 9 = 18 ... 9 x 1 = 9 9 x 2 = 18 ... 9 x 9 = 81 ``` 2. **編寫一個程式,讓使用者反覆輸入數字,直到輸入 0 為止,最後顯示所有輸入的數字的平均值** ```python numbers = [] while True: num = int(input("請輸入一個數字(輸入負數結束):")) if num == 0: break numbers.append(num) if numbers: average = sum(numbers) / len(numbers) print("平均值是:", average) else: print("沒有輸入有效的數字。") ``` - **執行範例**: ``` 請輸入一個數字(輸入負數結束):5 請輸入一個數字(輸入負數結束):10 請輸入一個數字(輸入負數結束):15 請輸入一個數字(輸入負數結束):-1 平均值是: 10.0 ``` --- ### 第六部分:結合 `for` 迴圈和 `if-else` 的小專案 #### 小專案:學生成績管理系統 **專案目標**: 建立一個簡單的成績管理系統,讓使用者輸入多位學生的姓名及分數,然後顯示每位學生的等級(例如:A、B、C 等),最後顯示班級的平均分數。 **專案步驟**: 1. **收集學生資料**: - 使用 `for` 迴圈來收集多位學生的姓名和分數。 - 可以預設學生人數,或者使用 `while` 迴圈讓使用者決定輸入多少學生。 2. **判斷等級**: - 使用 `if-else` 判斷每位學生的分數,並分配等級。 3. **計算平均分數**: - 使用迴圈來計算所有學生的分數總和,然後計算平均分數。 4. **顯示結果**: - 列出每位學生的姓名、分數及等級。 - 顯示班級的平均分數。 **範例程式碼**: ```python # 步驟一:收集學生資料 students = [] num_students = int(input("請輸入學生人數:")) for i in range(num_students): name = input(f"輸入第 {i+1} 位學生的姓名:") score = float(input(f"輸入 {name} 的分數:")) students.append({'name': name, 'score': score}) # 步驟二:判斷等級 for student in students: score = student['score'] if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'F' student['grade'] = grade # 步驟三:計算平均分數 total_score = 0 for student in students: total_score += student['score'] average_score = total_score / num_students # 步驟四:顯示結果 print("\n學生成績及等級:") for student in students: print(f"姓名:{student['name']},分數:{student['score']},等級:{student['grade']}") print(f"\n班級平均分數:{average_score:.2f}") ``` **執行範例**: ``` 請輸入學生人數:3 輸入第 1 位學生的姓名:小明 輸入 小明 的分數:85 輸入第 2 位學生的姓名:小華 輸入 小華 的分數:92 輸入第 3 位學生的姓名:小美 輸入 小美 的分數:76 學生成績及等級: 姓名:小明,分數:85.0,等級:B 姓名:小華,分數:92.0,等級:A 姓名:小美,分數:76.0,等級:C 班級平均分數:84.33 ```

    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