# wk10_1109_hackMd_函式與模組
<pre>
ch00_hackMd 書本模式-期末筆記
1. 封面、首頁、目錄
書本模式 /
如何使用書本模式: 書標題=== 、章標題 ---
幫筆記加上tag, title
分享所有人 (或amanda@mail.cgu.edu.tw 、TA政胤、TA祐愷)
ex : amanda'sBook_python
2. 筆記內頁
瀏覽模式
筆記管理 / 匯出匯入筆記 or copy(md) / paste(hackMd)
幫筆記加上tag, title
分享 : 所有人 (或amanda@mail.cgu.edu.tw 、TA政胤、TA祐愷)
ch07_函式與模組
7.1 自訂函式
1. 自訂函式
2. 參數預設值
3. 變數有效範圍
7.5 時間模組
1. 時間模組函式整理
2. 取得時間訊息函式
3. 執行程式相關時間函式
<pre/>
## 【inclass practice】
```python
# 單字和定義的字典
word_dict = {
"apple": "a round fruit with red or green skin and sweet flesh",
"dog": "a domesticated mammal that is related to the wolves",
"computer": "an electronic device for storing and processing data",
"ocean": "a large body of saltwater that covers most of the Earth's surface",
"book": "a written or printed work consisting of pages glued or sewn together along one side and bound in covers",
"mountain": "a large natural elevation of the earth's surface rising abruptly from the surrounding level"}
print("歡迎來到英文單字練習!")
score = 0
for word, definition in word_dict.items() :
print(f"定義: {definition}")
user_input = input(f"請輸入此定義的單字拼寫 : ").strip().lower() #.strip()刪除開頭和结尾的空白
if user_input == word :
print("正確!")
score += 1
else :
print(f"錯誤。正確答案是 '{word}'。")
print(f"練習結束。您的得分是 {score}/{len(word_dict)}。")
```
歡迎來到英文單字練習!
定義: a round fruit with red or green skin and sweet flesh
請輸入此定義的單字拼寫 : apple
正確!
定義: a domesticated mammal that is related to the wolves
請輸入此定義的單字拼寫 : dog
正確!
定義: an electronic device for storing and processing data
請輸入此定義的單字拼寫 : boook
錯誤。正確答案是 'computer'。
定義: a large body of saltwater that covers most of the Earth's surface
```python
import random
word_dict = {
"apple": "蘋果",
"banana": "香蕉",
"cat": "貓",
"dog": "狗",
"elephant": "大象",
"flower": "花朵",
"guitar": "吉他" }
score = 0
while True : #從單詞庫中隨機選擇一個單詞
word, meaning = random.choice(list(word_dict.items()))
print(f"請問 '{word}' 的中文意思是?")
player_guess = input("請輸入中文意思: ")
if player_guess == meaning : #檢查答案是否正確
print("正確!加10分。\n")
score += 10
else:
print(f"答錯了,正確答案是 '{meaning}'。\n")
print(f"你的分數: {score} 分\n") #顯示分數
play_again = input("是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): ") #問用戶是否繼續遊戲
if play_again.lower() == 'quit':
break
print("遊戲結束,你的最終分數是:", score)
```
請問 'flower' 的中文意思是?
請輸入中文意思: 花
答錯了,正確答案是 '花朵'。
你的分數: 0 分
是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 5
請問 'cat' 的中文意思是?
請輸入中文意思: 貓
正確!加10分。
你的分數: 10 分
是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 5
請問 'apple' 的中文意思是?
請輸入中文意思: 蘋果
正確!加10分。
你的分數: 20 分
是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): quit
```python
```
```python
def sayhello() :
print ("Hello, welcome!!!")
sayhello()
```
Hello, welcome!!!
```python
def getarea(w, h) :
area = w * h
return area
print(getarea(5, 10))
area1 = getarea(h = 10, w = 3)
print(area1)
```
50
30
```python
def sayhello2(name = "my friend") :
print("Hello, ", name, name)
print("I love", name, name)
sayhello2("黃同學")
sayhello2()
```
Hello, 黃同學 黃同學
I love 黃同學 黃同學
Hello, my friend my friend
I love my friend my friend
```python
def circle(radius) :
area = radius * radius * 3.14
length = 2 * radius * 3.14
return area, length
print(circle(5))
print(circle(10))
```
(78.5, 31.400000000000002)
(314.0, 62.800000000000004)
```python
def ctof(x) :
f = 1.8 * x + 32
return f
c = float(input("請輸入攝氏溫度 : "))
f = ctof(c)
print(f"攝氏 {c} 度 ; 華氏 {f} 度")
```
請輸入攝氏溫度 : 58.6
攝氏 58.6 度 ; 華氏 137.48000000000002 度
```python
import time
print(time.time())
print(time.localtime())
```
1699526010.842826
time.struct_time(tm_year=2023, tm_mon=11, tm_mday=9, tm_hour=18, tm_min=33, tm_sec=30, tm_wday=3, tm_yday=313, tm_isdst=0)
## 【afterclass practice】
<pre>
1.綜合演練 選擇題1-10 (需抄題在markdown cell;有程式碼的題目要有code cell)
2.教學影音:
新手入門 #07 Function (函式)
新手入門 #09 Module (模組)
<pre/>
( 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
```python
print(max([4,8,3,9,2,6]))
```
9
( B ) 3. print(pow(2,5,7)) 顯示的結果為何?
(A)2 (B)4 (C)5 (D)7
```python
print(pow(2,5,7))
```
4
( A ) 4. print("hospital".replace("s","t")) 顯示的結果為何?
(A)hotpital (B)hospisal (C)hospital (D)hotpisal
```python
print("hospital".replace("s","t"))
```
hotpital
( A ) 5. print("hospital".startswith("ho")) 顯示的結果為何?
(A)True (B)False (C)hospital (D)ho
```python
print("hospital".startswith("ho"))
```
True
( C ) 6. print("hospital".find("p")) 顯示的結果為何?
(A)-1 (B)0 (C)3 (D)4
```python
print("hospital".find("p"))
```
3
( A ) 7. 下列何者不可能是 print(random.randint(1,10)) 的顯示結果?
(A)0 (B)5 (C)8 (D)10
```python
import random
print(random.randint(1,10))
```
5
( D ) 8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果?
(A)0 (B)3 (C)12 (D)15
```python
import random
print(random.randrange(0,15,3))
```
6
( 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