# wk10_1109函式與模組 <pre> ## 【inclass practice】 {範例} 1. 攝氏溫度轉華氏溫度 \ 2. 學生分蘋果 \ 3. 總和及排序 \ 4. 檢查網址格式 \ 5. 以字串排版函式列印成績單 \ 6. 轉換日期格式 \ 7. 擲骰子遊戲 \ 8. 大樂透中獎號碼 \ 9. 列印時間函式所有資訊 \ 10. 計算執行一百萬次整數運算的時間 \ 11. {補充練習} 1A2B 函式1 : 數字相同+位置相同 函式2 : 數字相同 + 位置不同 In [6]: def cTOF(n) : f = 1.8 * n +32 return f c = float(input("輸入攝氏溫度")) f = cTOF(c) print(f"攝氏{c}度) = 華氏{f}") 輸入攝氏溫度30 攝氏30.0度) = 華氏86.0 In [7]: def sayhello( name ) : print("hello, 歡迎光臨", name) sayhello("booooo" ) hello, 歡迎光臨 booooo In [15]: def getArea(m, n=10) : area = m * n return area area = getArea(10,5) print("面積=", area) 面積= 50 In [19]: def circle(radius) : length = radius * 2 *3.14 area = radius * radius *3.14 return length, area r = 5 l, a = circle(r) print(f"半徑{r}, 圓周{l}, 面積{a}") 半徑5, 圓周31.400000000000002, 面積78.5 </pre> <pre> ## {綜合演練} 實作3: 以十二小時(上午、下午)顯示現在時刻。 實作4: 小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。 In [21]: import time as t print(t.time()) print(t.localtime()) 1699512833.0136719 time.struct_time(tm_year=2023, tm_mon=11, tm_mday=9, tm_hour=14, tm_min=53, tm_sec=53, tm_wday=3, tm_yday=313, tm_isdst=0) In [32]: def shoeNow(t) : h = time1 tm_hour if h < 12 : now = "上午" else : noe = "下午" h = h - 12 #print(time1) print(f"現在時間 : {now} {h}點{time1.tm_min}分{time1.tm_sec} </pre> <pre> ## 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音: 新手入門 #07 Function (函式) 新手入門 #09 Module (模組) 【OMP feedback】 ( 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 </pre>
{"description":"wk10_1109函式與模組【inclass practice】","contributors":"[{\"id\":\"4f0ea7d8-a994-4abc-a066-938bfab2315c\",\"add\":2312,\"del\":9}]","title":"wk10"}
Expand menu