## wk11_1116_ch07_函式模組_期末專題參考 ## 【inclass practice】 ch07_函式與模組 7.1 自訂函式 自訂函式 參數預設值 變數有效範圍 7.2 數值函式 數值函式整理 指數、商數、餘數及四捨六入 最大值、最小值、總和及排序 7.3 字串函式 字串函式整理 連接及分割字串 檢查起始或結束字串 字串排版相關函式 搜尋即取代字串 7.4 亂數模組 import 模組 亂數模組函式整理 產生整數或浮點數的亂數函式 隨機取得字元或串列元素 7.5 時間模組 時間模組函式整理 取得時間訊息函式 執行程式相關時間函式 ```python #執行計算一百萬次整數運算的時間 import time print("hello......") time.sleep(0.3) print("amanda!") ``` hello...... amanda! ```python import time as t print("hello......") t.sleep(0.3) print("amanda!") ``` hello...... amanda! ```python from time import sleep, ctime, localtime, perf_counter print("hello......") sleep(0.3) print("amanda!") ``` hello...... amanda! ```python from time import sleep, ctime, localtime, perf_counter startTime=perf_counter() for i in range(4): print(i) sleep(0.86) endTime=perf_counter() print(f"總運算時間,{endTime-startTime}") ``` 0 1 2 3 總運算時間,3.4805981000008615 ```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=48, tm_sec=41, tm_wday=3, tm_yday=320, tm_isdst=0) Thu Nov 16 10:48:41 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開始擲5 1 enter結束,按任意建後enter開始擲 遊戲結束