# wk10_1109_hackMd_函式與模組(1) ## hackMd 使用教學 1. 封面、首頁、目錄 : 書本模式 / 如何使用書本模式: 書標題=== 、章標題 --- 幫筆記加上tag, title 2. 筆記內頁 : 瀏覽模式 筆記管理 / 匯出匯入筆記 or copy(md)/ paste (hackMd) 幫筆記加上tag, title ## ch07_函式與模組 7.1 自訂函式 1. 自訂函式 2. 參數預設值 3. 變數有效範圍 7.2 數值函式 數值函式整理 指數、商數、餘數及四捨六入 最大值、最小值、總和及排序 7.3 字串函式 字串函式整理 連接及分割字串 檢查起始或結束字串 字串排版相關函式 搜尋即取代字串 7.4 亂數模組 import 模組 亂數模組函式整理 產生整數或浮點數的亂數函式 隨機取得字元或串列元素 7.5 時間模組 時間模組函式整理 取得時間訊息函式 執行程式相關時間函式 ```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拿掉前後空格 #lower全部小寫 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 ```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) ``` 請問 'elephant' 的中文意思是? 請輸入中文意思: 大象 正確!加10分。 你的分數: 10 分 是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 46 請問 'banana' 的中文意思是? 請輸入中文意思: 香蕉 正確!加10分。 你的分數: 20 分 是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): quit 遊戲結束,你的最終分數是: 20 ## [Afterclass Practice] 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 ```python import random print(random.randint(1,10)) ``` 4 ...沒有0 D 8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果? (A)0 (B)3 (C)12 (D)15 ```python print(random.randrange(0,15,3)) ``` 12 ```python print(random.randrange(0,15,3)) ``` 0 沒有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