## 【wk10_1109】hackMd_函式與模組 ## 【inclass practice】 ## 【ch07_函式與模組】 7.1 自訂函式 - 自訂函式 - 參數預設值 - 變數有效範圍 7.5 時間模組 - 時間模組函式整理 - 取得時間訊息函式 - 執行程式相關時間函式 #### {範例} 1. 攝氏溫度轉華氏溫度 \<ctof> 2. 學生分蘋果 \<divmod> 3. 總和及排序 \<sorted> 4. 檢查網址格式 \<startswith> 5. 以字串排版函式列印成績單 \<just> 6. 轉換日期格式 \<replace> 7. 擲骰子遊戲 \<randint> 8. 大樂透中獎號碼 \<sample> 9. 列印時間函式所有資訊 \<localtime> 10. 計算執行一百萬次整數運算的時間 \<pcounter> ```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' 退出遊戲,其他任意鍵繼續): 請問 'apple' 的中文意思是? 請輸入中文意思: 蘋果 正確!加10分。 你的分數: 20 分 是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 請問 'banana' 的中文意思是? 請輸入中文意思: 香蕉 正確!加10分。 你的分數: 30 分 是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): quit 遊戲結束,你的最終分數是: 30 ```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(10, 3) print(area1) ``` 50 30 ```python # 自訂函式 # 參數預設值 def sayHello2(name = "my friend") : print("hello, ", name, name) print("I love ", name) sayHello2("Chanel") sayHello2() ``` hello, Chanel Chanel I love Chanel hello, my friend my friend I love my friend ```python # 自訂函式 # 參數預設值 def Circle(radius) : area = radius * radius * 3.14 length = 2 * 3.14 * radius return area, length print(Circle(5)) print(Circle(10)) ``` (78.5, 31.400000000000002) (314.0, 62.800000000000004) ```python # 攝氏溫度轉華氏溫度 <ctof> def cToF(x) : f = 1.8 * x + 32 return f c = float(input("請輸入攝氏溫度: ")) f = cToF(c) print(f"攝氏度 {c}: 華氏{f} 度") ``` 請輸入攝氏溫度: -40 攝氏度 -40.0: 華氏-40.0 度 ```python import time print(time.time()) print(time.localtime()) ``` 1699763694.0807357 time.struct_time(tm_year=2023, tm_mon=11, tm_mday=12, tm_hour=4, tm_min=34, tm_sec=54, tm_wday=6, tm_yday=316, tm_isdst=0) ## 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音: - 新手入門 #07 Function (函式) - 新手入門 #09 Module (模組) ### 【綜合演練】 #### 1. 函式的傳回值,下列何者正確? ( A ) 無傳回值 ( B ) 1 個傳回值 ( C ) 2 個傳回值 ( D ) 以上皆可 答: ( D ) 以上皆可 #### 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 #### 3. print(pow(2,5,7)) 顯示的結果為何? ( A ) 2 ( B ) 4 ( C ) 5 ( D ) 7 ```python print(pow(2,5,7)) ``` 4 #### 4. print("hospital".replace("s","t")) 顯示的結果為何? ( A ) hotpital ( B ) hospisal ( C ) hospital ( D ) hotpisal ```python print("hospital".replace("s","t")) ``` hotpital #### 5. print("hospital".startswith("ho")) 顯示的結果為何? ( A ) True ( B ) False ( C ) hospital ( D ) ho ```python print("hospital".startswith("ho")) ``` True #### 6. print("hospital".find("p")) 顯示的結果為何? ( A ) -1 ( B ) 0 ( C ) 3 ( D ) 4 ```python print("hospital".find("p")) ``` 3 #### 7. 下列何者不可能是 print(random.randint(1,10)) 的顯示結果? ( A ) 0 ( B ) 5 ( C ) 8 ( D ) 10 ```python import random random.randint(1,10) # 生成指定範圍內的隨機整數 # 範圍是從 1 到 10(包含 1 和 10) print(random.randint(1,10)) ``` 10 答: (A) 0 #### 8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果? ( A ) 0 ( B ) 3 ( C ) 12 ( D ) 15 ```python (random.randrange(0,15,3)) # 表示生成一個從 0 開始、不包含 15、以步進值為 3 的隨機整數 # 可能的輸出包括 0、3、6、9、12 等數字,但不包括 15 print(random.randrange(0,15,3)) ``` 12 答: (D) 15 #### 9. 下列哪一個函式可讓程式停止執行一段時間? ( A ) time ( B ) sleep ( C ) perf_counter ( D ) localtime 答: (B) sleep #### 10. localtime 傳回的 tm_min 資料範圍為何? ( A ) 1到60 ( B ) 0到60 ( C ) 0到59 ( D ) 1到59 答: (C) 0到59 ### 【新手入門 #07 Function (函式)】 ```python def 打招呼(姓名, 年紀): print(f"{姓名}今年是{年紀}歲") 打招呼("小名", 20) 打招呼(年紀 = 20, 姓名 = "小名") ``` 小名今年是20歲 小名今年是20歲 ```python 折扣 = 0.9 def 總金額 (售價, 運費): return int(售價 * 折扣 + 運費) print(f"總金額為 {總金額(售價 = 1000, 運費 = 50)} 元") ``` 總金額為 950 元 ### 【新手入門 #09 Module (模組) 】 ```python import random 數字 = [1, 2, 3, 4, 5, 6,] random.shuffle(數字) a = random.choice(數字) b = random.randint(10, 50) print(數字) print(a) print(b) ``` [3, 2, 6, 4, 1, 5] 4 42 ```python import random import string 數字 = string.digits 英文 = string.ascii_letters 字母表 = 數字 + 英文 print(字母表) ``` 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ```python import random import string 數字 = string.digits 英文 = string.ascii_letters 字母表 = list(數字 + 英文) random.shuffle(字母表) print(字母表) ``` ['W', 't', 'o', 'k', '3', 'K', 'N', 'n', 'r', '1', '2', '5', 'e', '9', 'H', 'p', '8', '7', 'Z', 'w', 'x', 'I', 'a', '6', 'M', 'X', 'P', 'm', 'F', 'C', 'f', 'L', '0', 's', 'U', 'V', 'R', 'g', 'y', 'B', 'i', 'd', 'l', 'z', 'Q', 'Y', 'O', 'G', 'S', 'v', 'T', 'A', '4', 'D', 'E', 'j', 'b', 'q', 'u', 'J', 'h', 'c'] ```python import random import string 數字 = string.digits 英文 = string.ascii_letters 字母表 = list(數字 + 英文) random.shuffle(字母表) 長度 = int(input("你的密碼要幾位數? ")) 密碼 = "".join(字母表[:長度]) print(f"你的密碼是: {密碼}") ``` 你的密碼要幾位數? 8 你的密碼是: 2lbiGau7 ## 【self practice】 ```python # pow():計算指數 # pow(2, 5, 7) 表示計算 2 的 5 次方除以 7 的餘數 pow(2,5,7) ``` 4 ```python # find():用於在字串中尋找指定子字串的第一次出現位置 print("hospital".find("p")) print("hospital".find("b")) #如果找不到 "b",find()方法會返回 -1 ``` 3 -1 #### localtime() - 年(year):包含完整的年份,例如 2023。 - 月(month):範圍從 1 到 12。 - 日(day):範圍從 1 到 31。 - 時(hour):範圍從 0 到 23。 - 分(minute):範圍從 0 到 59。 - 秒(second):範圍從 0 到 61,允許有閏秒。 ```python import time local_time = time.localtime() print("年:", local_time.tm_year) print("月:", local_time.tm_mon) print("日:", local_time.tm_mday) print("時:", local_time.tm_hour) print("分:", local_time.tm_min) print("秒:", local_time.tm_sec) ``` 年: 2023 月: 11 日: 12 時: 5 分: 4 秒: 45 ```python import time # 獲取當地時間的 struct_time 對象 local_time = time.localtime() # 顯示星期幾 print("今天是星期", local_time.tm_wday) # 表示星期幾(範圍是 0 到 6,其中 0 是星期一) print("今天是一年中的第", local_time.tm_yday, "天") # 顯示一年中的第幾天(範圍是 1 到 366) ``` 今天是星期 6 今天是一年中的第 316 天