# wk09_1102_字典_期末筆記hackMd
<pre>
6.1 字典基本操作
1. 建立字典
2. 字典取值
3. 字典維護
6.2 字典進階操作
1.字典進階功能整理
2.in 功能
3.keys 及 values 方法
4.items 方法
5.setdefault 方法
<pre/>
## 【inclass practice】
### {範例}
1. 血型個性查詢 \<dictget>
2. 輸入及查詢學生成績 \<in>
3. keys 及 values 顯示世大運獎牌數 \<keyvalue>
4. intems 顯示世大運獎牌數 \<item>
```python
#1
dict_血型個性 = {"A":"成熟穩重",
"B":"活潑可愛",
"O":"自信勇敢",
"AB":"自然聰明"}
blood = (input("請輸入你的血型 : ")).upper() #.upper()讓輸入的字母變大寫
if dict_血型個性.get(blood) != None : #None第一個字母要大寫!
print("%s 的個性 : %s"%(blood, dict_血型個性[blood]))
else :
print("不存在的血型", blood)
```
請輸入你的血型 : o
O 的個性 : 自信勇敢
```python
print(dict_血型個性.items())
print(type(dict_血型個性.items()))
```
dict_items([('A', '成熟穩重'), ('B', '活潑可愛'), ('O', '自信勇敢'), ('AB', '自然聰明')])
<class 'dict_items'>
```python
print(dict_血型個性.keys())
print(type(dict_血型個性.keys()))
```
dict_keys(['A', 'B', 'O', 'AB'])
<class 'dict_keys'>
```python
print(dict_血型個性.values())
print(type(dict_血型個性.values()))
```
dict_values(['成熟穩重', '活潑可愛', '自信勇敢', '自然聰明'])
<class 'dict_values'>
```python
print("O" in dict_血型個性)
print("Z" in dict_血型個性)
```
True
False
```python
#2
dict_學生成績 = {"amy":75,
"emily":68,
"lin":52,
"zoe":87}
student = (input("請輸入學生姓名 : ")).lower()
#if dict_學生成績.get(student) != None :
# print("%s 的個性 : %d"%(student, dict_學生成績[student]))
#else :
# score = int(input("請輸入成績 : "))
# dict_學生成績.setdefault(student, score)
if student in dict_學生成績 :
print("%s 的成績 : %d"%(student, dict_學生成績[student]))
else :
score = int(input("請輸入成績 : "))
dict_學生成績.setdefault(student, score) #此setdefault沒鍵、有預設值,所以加入元素
print(dict_學生成績)
```
請輸入學生姓名 : Gray
請輸入成績 : 99
{'amy': 75, 'emily': 68, 'lin': 52, 'zoe': 87, 'gray': 99}
```python
#3
dict_世大運獎牌數 = {"金牌":51,
"銀牌":26,
"銅牌":37}
print("2023世大運")
for i in range (len(dict_世大運獎牌數)) :
print("%s數 : %d 面"%(list(dict_世大運獎牌數.keys())[i], list(dict_世大運獎牌數.values())[i]))
print("-------------")
for name, num in dict_世大運獎牌數.items() :
print("%s數: %d面"%(name, num))
#print("%s數 : %d\n"%(key, dict_世大運獎牌數[key]))
```
2023世大運
金牌數 : 51 面
銀牌數 : 26 面
銅牌數 : 37 面
-------------
金牌數: 51面
銀牌數: 26面
銅牌數: 37面
```python
#4
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
請輸入此定義的單字拼寫 : dog
正確!
定義: an electronic device for storing and processing data
請輸入此定義的單字拼寫 : boook
錯誤。正確答案是 'computer'。
定義: a large body of saltwater that covers most of the Earth's surface
## 【afterclass practice】
複習 list、tuple、dictionary