## wk11_1116_函式模組_期末專題參考 黃冠維 ch07_函式與模組 7.1 自訂函式 1. 自訂函式 2. 參數預設值 3. 變數有效範圍 7.2 數值函式 1. 數值函式整理 2. 指數、商數、餘數及四捨六入 3. 最大值、最小值、總和及排序 7.3 字串函式 1. 字串函式整理 2. 連接及分割字串 3. 檢查起始或結束字串 4. 字串排版相關函式 5. 搜尋即取代字串 7.4 亂數模組 1. import 模組 2. 亂數模組函式整理 3. 產生整數或浮點數的亂數函式 4. 隨機取得字元或串列元素 7.5 時間模組 1. 時間模組函式整理 2. 取得時間訊息函式 3. 執行程式相關時間函式 ## [inclass practice] ### {綜合演練} 實作3: 以十二小時(上午、下午)顯示現在時刻。 實作4: 小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。 ### {範例} 1. 攝氏溫度轉華氏溫度 \ 2. 學生分蘋果 \ 3. 總和及排序 \ 4. 檢查網址格式 \ 5. 以字串排版函式列印成績單 \ 6. 轉換日期格式 \ 7. 擲骰子遊戲 \ 8. 大樂透中獎號碼 \ 9. 列印時間函式所有資訊 \ 10. 計算執行一百萬次整數運算的時間 \ ### {補充練習} 1A2B 函式1 : 數字相同+位置相同 函式2 : 數字相同 + 位置不同 ```python #計算執行一百萬次整數運算的時間 \ import time as t print("hello.......") t.sleep(3) print("Wayne!") ``` hello....... Wayne! time.sleep,可以延緩幾秒執行 ```python from time import sleep, ctime, localtime, perf_counter print("hello.......") sleep(3) print("Wayne!") ``` hello....... Wayne! ```python from time import sleep, ctime, localtime, perf_counter start_time = perf_counter() for i in range(11) : print(i) sleep(0.994437) end_time = perf_counter() print(f"總運算時間, {end_time-start_time}") ``` 0 1 2 3 4 5 6 7 8 9 10 總運算時間, 11.016824500000439 perf_counter記得要加括號,不然會被當作一個變數 有趣的是,透過perf_counter可以得到一秒一數的時間會超過11秒,因為列印出來也要時間 ```python time1 = localtime() time2 = ctime() print(time1,"\n", time2) ``` time.struct_time(tm_year=2023, tm_mon=11, tm_mday=16, tm_hour=10, tm_min=47, tm_sec=57, tm_wday=3, tm_yday=320, tm_isdst=0) Thu Nov 16 10:47:57 2023 ```python import random as r while True : inkey = input("enter結束, 按任意鍵後enter開始擲骰子 ") if len(inkey) > 0 : print(r.randint(1,6)) else : print("遊戲結束") break ``` enter結束, 按任意鍵後enter開始擲骰子 4 6 enter結束, 按任意鍵後enter開始擲骰子 5 4 enter結束, 按任意鍵後enter開始擲骰子 6 2 enter結束, 按任意鍵後enter開始擲骰子 7 1 enter結束, 按任意鍵後enter開始擲骰子 1 2 enter結束, 按任意鍵後enter開始擲骰子 遊戲結束 ```python ! pip install numpy pandas ``` Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: numpy in c:\programdata\anaconda3\lib\site-packages (1.23.5) Requirement already satisfied: pandas in c:\programdata\anaconda3\lib\site-packages (1.5.3) Requirement already satisfied: python-dateutil>=2.8.1 in c:\programdata\anaconda3\lib\site-packages (from pandas) (2.8.2) Requirement already satisfied: pytz>=2020.1 in c:\programdata\anaconda3\lib\site-packages (from pandas) (2022.7) Requirement already satisfied: six>=1.5 in c:\programdata\anaconda3\lib\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0) ! 打指令給notebook ### {補充} ## [afterclass practice]