{%hackmd @themes/orangeheart %} ## wk09_1102_字典、hackMd 書本模式 ### 醫放三 B1003210 應雨岑 6.1 字典基本操作 1. 建立字典 2. 字典取值 3. 字典維護 6.2 字典進階操作 1. 字典進階功能整理 2. in 功能 3. keys 及 values方法 4. items 方法 5. setdefault 方法 ## 今日課程內容 <pre> 1. 5.5 串列排序 2. 5.6 串列常用的方法列表 3. 5.7 元組(Tuple) 4. 6.1 字典基本操作 </pre> ## NOTE <pre> dict_血型個性={"A":"內向穩重","B":"開朗樂觀","O":"堅強自信","AB":"聰明自然"} </pre> ## [ INCLASS PRACTICE ] * 1.血型個性查詢 \<dictget> ```python dict_血型個性={"A":"內向穩重", "B":"開朗樂觀", "O":"堅強自信", "AB":"聰明自然"} print(dict_血型個性) print(dict_血型個性.items()) print(dict_血型個性.keys()) print(dict_血型個性.values()) print(type(dict_血型個性.items())) print("A" in dict_血型個性) ``` {'A': '內向穩重', 'B': '開朗樂觀', 'O': '堅強自信', 'AB': '聰明自然'} dict_items([('A', '內向穩重'), ('B', '開朗樂觀'), ('O', '堅強自信'), ('AB', '聰明自然')]) dict_keys(['A', 'B', 'O', 'AB']) dict_values(['內向穩重', '開朗樂觀', '堅強自信', '聰明自然']) <class 'dict_items'> True * 血型個性查詢2 \<dictget> ```python """ 不同血型的人具有不同的個性:設計程式建立 4 筆字典資料: 「鍵」為血型,「值」為個性。 使用者輸入血型後,若血型存在,就顯示該血型的個性,如果血型不存在,則顯示沒有該血型的訊息。 """ dict_血型個性={"A":"內向穩重","B":"開朗樂觀","O":"堅強自信","AB":"聰明自然"} blood=(input("請輸入血型")).upper() #小寫也可以辨別,因為變成大寫了 if dict_血型個性.get(blood)!=None: result= dict_血型個性[blood] print("%s型的個性為%s" %(blood,result)) else: print("%s血型不存在"%blood) ``` 請輸入血型D D血型不存在 * 輸入及查詢學生成績 \<in> ```python """ 為了解學習狀況,老師需要查詢學生成績。設計程式建立 3 筆字典資料:「鍵」為學生姓名,「值」為學生成績。 老師輸入學生姓名後,若學生姓名存在,就顯示該學生成績,否則就讓老師輸入成績,並將學生資料加入字典 """ dict_成績={"alise":100,"bob":50,"cindy":85,"david":99} student=(input("請輸入學生姓名")).lower() #都變小寫而可辨別 if dict_成績.get(student)!=None: print("%s學生成績為%s" %(student,dict_成績[student])) else: print("%s學生的成績不存在"%student) score=int(input("請輸入該學生成績")) dict_成績.setdefault(student,score) print(dict_成績) ``` 請輸入學生姓名sds sds學生得成績不存在 請輸入該學生成績12 {'alise': 100, 'bob': 50, 'cindy': 85, 'david': 99, 'sds': 12} ```python dict_成績={"alise":100,"bob":50,"cindy":85,"david":99} student=(input("請輸入學生姓名")).lower() #都變小寫而可辨別 if student in dict_成績: print("%s學生成績為%s" %(student,dict_成績[student])) else: print("%s學生的成績不存在"%student) score=int(input("請輸入該學生成績")) dict_成績.setdefault(student,score) print(dict_成績) ``` 請輸入學生姓名dcc dcc學生的成績不存在 請輸入該學生成績12 {'alise': 100, 'bob': 50, 'cindy': 85, 'david': 99, 'dcc': 12} * keys 及 values 顯示世大運獎牌數 \<keyvalue> ```python """ 台灣主辦 2017 年世界大學運動會,成績輝煌。 請建立 3 筆字典資料:「鍵」為獎牌名稱,「值」為獎牌數,再使用 keys 及 values 功能顯示各種獎牌數。 """ dict_獎牌數={"金牌":51,"銀牌":26,"銅牌":37} #name=input("請輸入什麼名次") #num=dict_獎牌數[name] print("2023世大運") for name, num in dict_獎牌數.items(): print("%s數為%s面" %(name,num)) print("---------------") for i in range(len(dict_獎牌數)): print("%s數為%s面" %(list(dict_獎牌數.keys())[i],list(dict_獎牌數.values())[i])) ``` 2023世大運 金牌數為51面 銀牌數為26面 銅牌數為37面 --------------- 金牌數為51面 銀牌數為26面 銅牌數為37面 * intems 顯示世大運獎牌數 \<item> ```python """ 請利用 items 功能來重新顯示 2017 年世界大學運動會中台灣代表隊的各種獎牌數。 """ dict_獎牌數={"金牌":51,"銀牌":26,"銅牌":37} print(dict_獎牌數) ``` {'金牌': 51, '銀牌': 26, '銅牌': 37} * 單字和定義的字典 ```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 請輸入單字的拼寫: dogs 錯誤。正確答案是 'dog'。 定義: an electronic device for storing and processing data 請輸入單字的拼寫: computer 正確! 定義: a large body of saltwater that covers most of the Earth's surface 請輸入單字的拼寫: sea 錯誤。正確答案是 'ocean'。 定義: a written or printed work consisting of pages glued or sewn together along one side and bound in covers 請輸入單字的拼寫: book 正確! 定義: a large natural elevation of the earth's surface rising abruptly from the surrounding level 請輸入單字的拼寫: mountains 錯誤。正確答案是 'mountain'。 練習結束。您的得分是 3/6。 ## 全部的note <pre> W1 計算符號之間可以加空白鍵 <br> ** : 幾次方<br> sep="." : 分隔符號為. end="/" : 結束用/結束 在print內寫入%d : 放入參數答案 (最後在答案要加%) %s : 放入的答案為字串 %f : 放入的答案包含全部小數點 %.1f : 包含 1位小數點 %.3f : 包含 3位小數點 寫法如下- print("身高為%d cm,體重為%d kg,BMI為%.1f " % (height,weight,bmi)) print("顯示的文字",參數名字)<br> my_height = input("請輸入身高(cm)") : 可以輸入文字進去對話框 使用者輸入的文字要轉換成"int" ()<br> W2 print("姓名 成績") print("%3s %3d"%(name1,score1)) #按照字元數進行規則排列出一格表格格式 W3 **次方 //整除的數 /除以 %餘數 and 放在兩個運算子中間,判斷True or False,當兩個都為True才會是True or 放在兩個運算子中間,判斷True or False,當兩個都為False才會是False W4 if,else,elif需使用冒號:還有縮排來寫code .upper可以讓程式不用判斷大小寫 W5 串列list用中括弧括起來 for和其他語法一樣,他同樣也是需要用到:還有縮排來寫程式 W6 break會跳出這個迴圈 continue跳出執行這個code 繼續執行下一個i的程式 None 為一個什麼都沒有的數字,但是他可以做加減乘除的運算 W7 串列名稱=[1,2,3,...] #可以放字串、數字等等的 另外也可以宣告空的串列list=[] 多為變數的宣告 list=[["1"],["1,2"],["1,2,3"]] 如果想要顯示第二個串列的第一個內容:print(list[1][0])) # 0表示第一個資料,1表示第二個-意思是程式計算的方式是由0開始數 若想檢索特定的元素可以使用list[起始索引,終止索引,間隔]的方式進行 也可以索引-值,表示從最後一個開始數list[-1] len(list)可以計算元素數量 max(list)可以找到元素內最大值 list.index(...)可以找...在清單中第幾個 .count 可以數...出現幾次 .append 插入一個元素在最後 .insert(3,8)插入8某一個位置(第三個) .pop 移除最後一個元素 W8 divmod(7,3) #答案為(2,1) 表示(商,餘數) 元組內的元素不能修改,但可以轉換成串列 字典是以大括號呈現{} 由小排到大 score.sort() 由大排到小 score.sort(reverse=True) 反轉是錯誤的則不用反轉,由小排到大 score.sort(reverse=False) </pre> ## [ afterclass practice ] > 1. 教學影音 : hackMd 教學 16:56標籤 18:26將筆記轉換成書本模式 > 2. 複習 list、tuple、dictionary * ## 教學影音 hackMd 教學 hackMd是一個可以在markdown裡面寫CSS 他的語法很多都像HTML,CSS一樣,如字體顏色、大小等等 <pre> 1. ##### tags:"test1","test2" 做成標籤的方式,而就可以將這篇筆記放在test1&test2的目錄裡面 2. 將各個筆記的超連結放入同一個筆記當中就可以變成書本的模式 (分享那將預設的瀏覽模式調整成書本模式,就可以讓其他人以書本模式去閱讀我們的筆記) </pre> * ## 複習 list、tuple、dictionary <pre> list:是串列,當有大量資料需儲存時就可以使用到 list=[1,2,3,4,5] tuple:是元組,它的結構和串列一樣,只是各個元素皆不能夠做修改,而被稱作不能修改的串列 tuple=(1,2,3,4,5) dictionary:是字典,和list最大不同是裡面的資料並沒有按照順序排列的,而是以"鍵-值"對的形式儲存資料 dict={"banana":50,"cherry":100} </pre> ## [ self practice ] * 實作5: ```python """ 目前台幣對美金、日幣和人民幣的兌換匯率分別是 美金:28.02、日幣:0.2513、人民幣:4.24。 請設計此匯率兌換程式,輸入台幣金額後計算可以兌換多少的美金、日幣和人民幣。 """ dict={"USD":28.02,"JP":0.2513,"RMB":4.24} dollar=int(input("請輸入台幣多少錢")) for i in range(len(dict)): print("台幣%d元可換成%s%.2f元" % (dollar,list(dict.keys())[i],dollar/list(dict.values())[i])) ``` 請輸入台幣多少錢10000 台幣10000元可換成USD356.89元 台幣10000元可換成JP39793.08元 台幣10000元可換成RMB2358.49元