### WK_10_B1121138_邱冠誠
ch00_hackMd 書本模式 - 期末筆記
hackMd 使用教學
封面、首頁、目錄
書本模式 /
如何使用書本模式: 書標題=== 、章標題 ---
幫筆記加上tag, title
分享所有人 (或amanda@mail.cgu.edu.tw 、TA政胤、TA祐愷)
ex : amanda'sBook_python
筆記內頁
瀏覽模式
筆記管理 / 匯出匯入筆記 or
copy(md)/ paste (hackMd)
幫筆記加上tag, title
分享 : 所有人 (或amanda@mail.cgu.edu.tw 、TA政胤、TA祐愷)
ch07_函式與模組
7.1 自訂函式
自訂函式
參數預設值
變數有效範圍
7.2 數值函式
數值函式整理
指數、商數、餘數及四捨六入
最大值、最小值、總和及排序
7.3 字串函式
字串函式整理
連接及分割字串
檢查起始或結束字串
字串排版相關函式
搜尋即取代字串
7.4 亂數模組
import 模組
亂數模組函式整理
產生整數或浮點數的亂數函式
隨機取得字元或串列元素
7.5 時間模組
時間模組函式整理
取得時間訊息函式
執行程式相關時間函式
【inclass practice】
{綜合演練}
實作3:
以十二小時(上午、下午)顯示現在時刻。
實作4:
小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。
{範例}
攝氏溫度轉華氏溫度 \<ctof>
學生分蘋果 \<divmod>
總和及排序 \<sorted>
檢查網址格式 \<startswith>
以字串排版函式列印成績單 \<just>
轉換日期格式 \<replace>
擲骰子遊戲 \<randint>
大樂透中獎號碼 \<sample>
列印時間函式所有資訊 \<localtime>
計算執行一百萬次整數運算的時間 \<pcounter>
{補充練習}
1A2B
函式1 : 數字相同+位置相同
函式2 : 數字相同 + 位置不同
【afterclass practice】
綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
教學影音:
新手入門 #07 Function (函式)
新手入門 #09 Module (模組)
```python
def cTOf(n):
f = 1.8 * n + 32
return f
c = float(input("輸入攝氏溫度"))
f = cTOf(c)
print(f"攝氏{c}度 = 華氏{f}度")
```
輸入攝氏溫度37
攝氏37.0度 = 華氏98.60000000000001度
```python
def sayHello(name = "idiot"):
print(name , "hello")
sayHello("baga")
sayHello()
```
baga hello
idiot hello
```python
def getArea(w , l):
return w * l
w = int(input("寬"))
l = int(input("高"))
print("面積是",getArea(w,l))
```
寬5
高5
面積是 25
```python
def getL(r):
return 2 * 3.14 * r
def getArea(r):
return r * r * 3.14
r = int(input("半徑"))
print("圓周是",getL(r))
print("園面積是",getArea(r))
```
半徑5
圓周是 31.400000000000002
園面積是 78.5
```python
def getCircle(r):
L = 2 * 3.14 * r
Area = r * r * 3.14
return L , Area
r = int(input("半徑"))
L , Area = getCircle(r)
print(f"半徑{r} , 圓周{L} , 園面積{Area}")
```
半徑5
半徑5 , 圓周31.400000000000002 , 園面積78.5
```python
time1 = t.localtime()
h = time1.tm_hour
if h > 12 :
now = "下午"
h -= 12
else :
now = "上午"
#print(time1)
print(f"現在時間:{now}{h}點{time1.tm_min}分{time1.tm_sec}秒")
```
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 1
----> 1 time1 = t.localtime()
3 h = time1.tm_hour
5 if h > 12 :
NameError: name 't' is not defined
( D ) 1. 函式的傳回值,下列何者正確?
(A)無傳回值 (B) 1 個傳回值 (C) 2 個傳回值 (D)以上皆可
( C ) 2. print(max([4,8,3,9,2,6])) 顯示的結果為何?
(A)4 (B)6 (C)9 (D)2
( B ) 3. print(pow(2,5,7)) 顯示的結果為何?
(A)2 (B)4 (C)5 (D)7
( A ) 4. print("hospital".replace("s","t")) 顯示的結果為何?
(A)hotpital (B)hospisal (C)hospital (D)hotpisal
( A ) 5. print("hospital".startswith("ho")) 顯示的結果為何?
(A)True (B)False (C)hospital (D)ho
( C ) 6. print("hospital".find("p")) 顯示的結果為何?
(A)-1 (B)0 (C)3 (D)4
( A ) 7. 下列何者不可能是 print(random.randint(1,10)) 的顯示結果?
(A)0 (B)5 (C)8 (D)10
( D ) 8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果?
(A)0 (B)3 (C)12 (D)15
( B ) 9. 下列哪一個函式可讓程式停止執行一段時間?
(A)time (B)sleep (C) perf_counter (D)localtime
( C ) 10. localtime 傳回的 tm_min 資料範圍為何?
(A)1到60 (B)0到60 (C)0到59 (D)1到59
```python
print(max([4,8,3,9,2,6]))
```
9
```python
print(pow(2,5,7))
```
4
```python
print("hospital".replace("s","t"))
```
hotpital
```python
print("hospital".startswith("ho"))
```
True
```python
print("hospital".find("p"))
```
3
```python
print(random.randint(1,10))
```
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 1
----> 1 print(random.randint(1,10))
NameError: name 'random' is not defined
```python
print(random.randrange(0,15,3))
```
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 1
----> 1 print(random.randrange(0,15,3))
NameError: name 'random' is not defined