```python WK_10 [inclass practice] ``` ```python {範例} 攝氏溫度轉華氏溫度 \<ctof> 學生分蘋果 \<divmod> 總和及排序 \<sorted> 檢查網址格式 \<startswith> 以字串排版函式列印成績單 \<just> 轉換日期格式 \<replace> 擲骰子遊戲 \<randint> 大樂透中獎號碼 \<sample> 列印時間函式所有資訊 \<localtime> 計算執行一百萬次整數運算的時間 \<pcounter> ``` ```python def cTof(n) : f = 1.8 * c + 32 return f c = float(input("請輸入攝氏溫度")) f = cTof(c) print(f"攝氏{c}度 = 華氏{f}度") ``` 請輸入攝氏溫度10 攝氏10.0度 = 華氏50.0度 ```python def sayHello() : print("hello") sayHello() ``` ```python def getArea(m,n=10): area1 = m*n return area1 area =getArea(10): print("面積=",area) ``` ```python def cicle(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}") ``` ```python import time print (time.time()) print(time.localtime()) ``` ```python def shownow(t) : time1 = t.localtime() h = time1.tm_hour if h > 12 : now = "上午" else : now = "下午" print(time1) print("現在時間 : 下午2點55分35秒") ``` ```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"請輸入單辭 '{word}' 的拼寫: ").strip().lower() if user_input == word: print("正確!") score += 1 else: print(f"錯誤。正確答案是 '{word}'。") print(f"練習結束。您的得分是 {score}/{len(word_dict)}。") ``` ```python random.choice(list(word_dict.items())) ``` ```python ``` ## [after class] ```python monthname = {1:'JAN', 2:'FEB', 3:'MAR', 4:'APR', 5:'MAY', 6:'JUN', 7:'JUL', 8:'AUG', 9:'SEP', 10:'OCT', 11:'NOV', 12:'DEC'} m = int(input("【查詢月份簡寫】\n請輸入要查詢的月份數字:")) print("{}月份的英文簡寫為 {}:".format(m, monthname[m])) ``` ```python cnum = {0:"零", 1:"壹", 2:"貮", 3:"參", 4:"肆", 5:"伍", 6:"陸", 7:"柒", 8:"捌", 9:"玖"} num = input("請輸入小於5位數的數字:") for n in num: print(cnum[int(n)], end="") ``` ```python dict1 = {"台北市":6, "新北市":2, "桃園市":5, "台中市":8, "台南市":3, "高雄市":9} name = input("輸入要查詢的六都名稱:") PM25 = dict1.get(name) if PM25 == None: print("六都中沒有「" + name + "」城市!") else: print(name + " 今天的 PM2.5 值為:" + str(dict1[name])) ``` ```python dict1 = {"鼠":"親切和藹", "牛":"保守努力", "虎":"熱情大膽", "兔":"溫柔仁慈"} for name, nature in dict1.items(): print("生肖屬 %s 的性格特癥為 %s" % (name, nature)) ``` 生肖屬 鼠 的性格特癥為 親切和藹 生肖屬 牛 的性格特癥為 保守努力 生肖屬 虎 的性格特癥為 熱情大膽 生肖屬 兔 的性格特癥為 溫柔仁慈 ```python rate = {'USD':28.02, 'JPY':0.2513, 'CNY':4.24} TWD = float(input("請輸入台幣:")) print("台幣{:.2f}元等於美金{:.2f}元, 日幣{:.2f}元, 人民幣{:.2f}元".format(TWD, TWD/rate['USD'], TWD/rate['JPY'], TWD/rate['CNY'])) ``` 請輸入台幣:2 台幣2.00元等於美金0.07元, 日幣7.96元, 人民幣0.47元 ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ```