## ch06_字典
6.1 字典基本操作
1. 建立字典
2. 字典取值
3. 字典維護
6.2 字典進階操作
1. 字典進階功能整理
2. in 功能
3. keys 及 values方法
4. items 方法
5. setdefault 方法
### 【inclass practice】
#### {概念複習}
#### {範例}
1. ~~血型個性查詢 \<dictget>~~
2. 輸入及查詢學生成績 \<in>
3. keys 及 values 顯示世大運獎牌數 \<keyvalue>
4. intems 顯示世大運獎牌數 \<item>
```python
dic1 = {"A":"內向","B":"外向","O":"自信","AB":"自然"}
#().upper()=使輸入小寫轉大寫
blood = (input("enter the blood type :")).upper()
if dic1.get(blood) != None :
print("血型 %s 的特質是: %s" % (blood,dic1[blood]))
else :
print("此血型不存在")
```
enter the blood type :y
此血型不存在
```python
dic2 = {"alice":29,"bob":82,"oliver":63,"andy":14}
student = (input("enter the student name :")).lower()
#if dic2.get(student) != None :
# print(" %s 的分數是: %s" % (student,dic2[student]))
#else :
# print("此學生不存在")
if student in dic2 :
print(" %s 的分數是: %d " % (student,dic2[student]))
else :
score = int(input("enter score?"))
dic2.setdefault(student,score)
print(dic2)
```
enter the student name :ann
enter score?45
{'alice': 29, 'bob': 82, 'oliver': 63, 'andy': 14, 'ann': 45}
```python
dic3 ={"金牌":29,"銀牌":63,"銅牌":82}
print("2017世大運成績如下:")
#個別取出鍵&值:強制轉型為list
list_key = list(dic3.keys())
list_value = list(dic3.values())
for i in range(len(dic3)) :
print("%s 得 %d 面" % (list_key[i],list_value[i]))
#個別取出鍵&值:name,num
for name,num in dic3.items():
print("%s 得 %d 面" % (name,num))
print(dic3.items())
print(dic3.keys())
print(dic3.values())
print(type(dic3.items()))
print(type(dic3.keys()))
print(type(dic3.values()))
```
2017世大運成績如下:
金牌 得 29 面
銀牌 得 63 面
銅牌 得 82 面
金牌 得 29 面
銀牌 得 63 面
銅牌 得 82 面
dict_items([('金牌', 29), ('銀牌', 63), ('銅牌', 82)])
dict_keys(['金牌', '銀牌', '銅牌'])
dict_values([29, 63, 82])
<class 'dict_items'>
<class 'dict_keys'>
<class 'dict_values'>
#### {綜合演練}
```
實作5:
目前台幣對美金、日幣和人民幣的兌換匯率分別是
美金:28.02、日幣:0.2513、人民幣:4.24。
請設計此匯率兌換程式,輸入台幣金額後計算可以兌換多少的美金、日幣和人民幣。
```
```python
tw = int(input("請輸入要兌換的台幣金額:"))
change ={"美金":us,"日幣":jp,"人民幣":ch}
us = float(28.02*tw)
jp = float(0.2513*tw)
ch = float(4.24*tw)
print(change)
```
請輸入要兌換的台幣金額:100
{'美金': 2802.0, '日幣': 25.130000000000003, '人民幣': 424.0}
#### {補充}
- 英文單字測驗
```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)}。")
```
歡迎來到英文單字練習!
定義: a round fruit with red or green skin and sweet flesh
請輸入單辭 'apple' 的拼寫: apple
正確!
定義: a domesticated mammal that is related to the wolves
請輸入單辭 'dog' 的拼寫: dog
正確!
定義: an electronic device for storing and processing data
請輸入單辭 'computer' 的拼寫: computer
正確!
定義: a large body of saltwater that covers most of the Earth's surface
請輸入單辭 'ocean' 的拼寫: ocean
正確!
定義: a written or printed work consisting of pages glued or sewn together along one side and bound in covers
請輸入單辭 'book' 的拼寫: book
正確!
定義: a large natural elevation of the earth's surface rising abruptly from the surrounding level
請輸入單辭 'mountain' 的拼寫: mountain
正確!
練習結束。您的得分是 6/6。
### 【afterclass practice】
- 教學影音 : hackMd 教學 16:56標籤 18:26將筆記轉換成書本模式
- 複習:list、tuple、dictionary