# wk11_1116_函式模組_期末專題參考
<pre>
ch07_函式與模組
7.2 數值函式
1. 數值函式整理
2. 指數、商數、餘數及四捨六入
3. 最大值、最小值、總和及排序
7.3 字串函式
1. 字串函式整理
2. 連接及分割字串
3. 檢查起始或結束字串
4. 字串排版相關函式
5. 搜尋即取代字串
7.4 亂數模組
1. import 模組
2. 亂數模組函式整理
3. 產生整數或浮點數的亂數函式
4. 隨機取得字元或串列元素
7.5 時間模組
1. 時間模組函式整理
2. 取得時間訊息函式
3. 執行程式相關時間函式
<pre/>
## 【inclass practice】
### {綜合演練}
<pre>
實作3:
以十二小時(上午、下午)顯示現在時刻。
實作4:
小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。
<pre/>
### {範例}
1. 學生分蘋果 \
2. 總和及排序 \
3. 檢查網址格式 \
4. 以字串排版函式列印成績單 \
5. 轉換日期格式 \
6. 擲骰子遊戲 \
7. 大樂透中獎號碼 \
8. 計算執行一百萬次整數運算的時間 \
```python
#8 計算執行一百萬次整數運算的時間
import time
print("Hello......")
time.sleep(1)
print("YuBao!")
```
Hello......
YuBao!
```python
import time as t #time簡寫成t
print("Hello......")
t.sleep(0.5)
print("YuBao!")
```
Hello......
YuBao!
```python
from time import sleep, ctime, localtime, perf_counter
print("Hello......")
sleep(1)
print("YuBao!")
```
Hello......
YuBao!
```python
startTime = perf_counter() #perf_counter取得程式執行的時間,這裡是程式開始執行到startTime的時間
for i in range(11) :
print(i)
sleep(0.3)
endTime = perf_counter() #startTime與endTime之間的時間
print(f"總運算時間, {endTime - startTime}")
```
0
1
2
3
4
5
6
7
8
9
10
總運算時間, 3.374068299999635
```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=6, tm_wday=3, tm_yday=320, tm_isdst=0)
Thu Nov 16 10:48:06 2023
```python
#6 擲骰子遊戲
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
6
enter結束,按任意鍵後enter開始擲 : 89
5
enter結束,按任意鍵後enter開始擲 : 77
2
enter結束,按任意鍵後enter開始擲 : 36
2
enter結束,按任意鍵後enter開始擲 : 1
2
enter結束,按任意鍵後enter開始擲 :
遊戲結束
### {補充練習}
<pre>
1A2B
函式1 : 數字相同+位置相同
函式2 : 數字相同 + 位置不同
<pre/>
## 【afterclass practice】
<pre>
1. 教學影音:
新手入門 #07 Function (函式)
新手入門 #09 Module (模組)
2. 期末分組名單
<pre/>