## week10_1109_函式與模組
### ch07_函式與模組
7.1 自訂函式<br>
1. 自訂函式<br>
2. 參數預設值<br>
3. 變數有效範圍<br>
7.2 數值函式<br>
1. 數值函式整理<br>
2. 指數、商數、餘數及四捨六入<br>
3. 最大值、最小值、總和及排序<br>
7.3 字串函式<br>
1. 字串函式整理<br>
2. 連接及分割字串<br>
3. 檢查起始或結束字串<br>
4. 字串排版相關函式<br>
5. 搜尋即取代字串<br>
7.4 亂數模組<br>
1. import 模組<br>
2. 亂數模組函式整理<br>
3. 產生整數或浮點數的亂數函式<br>
4. 隨機取得字元或串列元素<br>
7.5 時間模組<br>
1. 時間模組函式整理<br>
2. 取得時間訊息函式<br>
3. 執行程式相關時間函式<br>
### [inclass practice]
### {範例}
```
1. 攝氏溫度轉華氏溫度
```
```python
def cTOf(n):
f = 1.8 * c + 32
return f
c = float(input("輸入攝氏溫度"))
f = cTOf(c)
print(f"攝氏{c}度 = 華氏{f}度")
```
輸入攝氏溫度15
攝氏15.0度 = 華氏59.0度
```python
def sayHello(name = "my friend"):
print(name, "hello, 歡迎光臨")
sayHello("Vivian")
sayHello()
```
Vivian hello, 歡迎光臨
my friend hello, 歡迎光臨
```python
def getArea(m, n = 10):
area1 = m * n
return area1
area = getArea(10)
print("面積=",area)
```
面積= 100
```python
def circle(radius):
length = radius * 2 * 3.14
area = radius * radius * 3.14
return length, area
r = 10
l, a = circle(r)
print(f"半徑{r}, 圓周{l}, 面積{a}")
```
半徑10, 圓周62.800000000000004, 面積314.0
```
9. 列印時間函式所有資訊
```
```python
import time as t
print(t.time())
print(t.localtime())
```
1699521393.6997018
time.struct_time(tm_year=2023, tm_mon=11, tm_mday=9, tm_hour=17, tm_min=16, tm_sec=33, tm_wday=3, tm_yday=313, tm_isdst=0)
```python
def showNow(t):
time1 = t.localtime()
h = time1.tm_hour
if h < 12:
now = "上午"
else:
now = "下午"
h = h - 12
# print(time1)
print(f"現在時間:{now} {h}點{time1.tm_min}分{time1.tm_sec}秒")
```
現在時間:下午 5點24分1秒
### [afterclass practice]
1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
2. 教學影音:
- 新手入門 #07 Function (函式)
- 新手入門 #09 Module (模組)
### {綜合演練}
1. 函式的傳回值,下列何者正確?<br>
(A)無傳回值 (B) 1 個傳回值 (C) 2 個傳回值 (D)以上皆可<br>
ans:(D)
2. print(max([4,8,3,9,2,6])) 顯示的結果為何?<br>
(A)4 (B)6 (C)9 (D)2<br>
ans:(C)
```python
print(max([4,8,3,9,2,6]))
```
9
3. print(pow(2,5,7)) 顯示的結果為何?<br>
(A)2 (B)4 (C)5 (D)7<br>
ans:(B)
```python
print(pow(2,5,7))
```
4
4. print("hospital".replace("s","t")) 顯示的結果為何?<br>
(A)hotpital (B)hospisal (C)hospital (D)hotpisal<br>
ans:(A)
```python
print("hospital".replace("s","t"))
```
hotpital
5. print("hospital".startswith("ho")) 顯示的結果為何?<br>
(A)True (B)False (C)hospital (D)ho<br>
ans:(A)
```python
print("hospital".startswith("ho"))
```
True
6. print("hospital".find("p")) 顯示的結果為何?<br>
(A)-1 (B)0 (C)3 (D)4<br>
ans:(C)
```python
print("hospital".find("p"))
```
3
7. 下列何者不可能是 print(random.randint(1,10)) 的顯示結果?<br>
(A)0 (B)5 (C)8 (D)10<br>
ans:(A)
8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果?<br>
(A)0 (B)3 (C)12 (D)15<br>
ans:(D)
9. 下列哪一個函式可讓程式停止執行一段時間?<br>
(A)time (B)sleep (C) perf_counter (D)localtime<br>
ans:(B)
10. localtime 傳回的 tm_min 資料範圍為何?<br>
(A)1到60 (B)0到60 (C)0到59 (D)1到59<br>
ans:(C)