---
# System prepended metadata

title: wk9
tags: [Python]

---

# ch06_字典_w9 高承紘 課堂程式練習

    6.1  字典基本操作

    1、建立字典
    2、字典取值
    3、字典維護
    
    6.2  字典進階操作

    1、字典進階功能整理
    2、in 功能
    3、keys 及 values方法
    4、items 方法
    5、setdefault 方法

## 【inclass practice】


```python
dict1={"A":"stupid",
       "B":"failure",
       "O":"ugly",
       "AB":"idiot"}

blood=input("你的血型是? ")

if(dict1.get(blood)!=None):
    print("%s血型的特質是%s" %(blood,dict1[blood]))
else:
    print("錯誤")
```

    你的血型是? AB
    AB血型的特質是idiot
    


```python
dict2={"Alice":90,
       "Bob":80,
       "Oscar":70,
       "Billy":100}

s=input("你的名字是? ")

if(dict2.get(s) !=None):
    print("%s的分數是%d分" %(s, dict2[s]))
if s in dict2:
    print("%s的分數是%d分" %(s, dict2[s]))
else:
    print("錯誤")
```

    你的名字是? Billy
    Billy的分數是100分
    Billy的分數是100分
    


```python
dict2={"Alice":90,
       "Bob":80,
       "Oscar":70,
       "Billy":100}

s=input("你的名字是? ")

if s in dict2:
    print("%s的分數是%d分" %(s, dict2[s]))
else:
    score=int(input("幾分"))
    dict2.setdefault(s,score)
print(dict2)
```

    你的名字是? bill
    幾分60
    {'Alice': 90, 'Bob': 80, 'Oscar': 70, 'Billy': 100, 'bill': 60}
    


```python
dict3={"金牌":90,
       "銀牌":80,
       "銅牌":70,
       "鐵牌":100}

print("%s 得 %s 面" %("金牌",dict3["金牌"]))
print(dict3.items())
print(dict3.keys())
print(dict3.values())

list_key=list(dict3.keys())

print(list_key[0])
```

    金牌 得 90 面
    dict_items([('金牌', 90), ('銀牌', 80), ('銅牌', 70), ('鐵牌', 100)])
    dict_keys(['金牌', '銀牌', '銅牌', '鐵牌'])
    dict_values([90, 80, 70, 100])
    金牌
    


```python
dict3={"金牌":90,
       "銀牌":80,
       "銅牌":70,
       "鐵牌":100}

list_key=list(dict3.keys())
list_value=list(dict3.values())

for i in range(len(dict3)):
    print("得到的%s有%d個" %(list_key[i],list_value[i]))
    
for name,num in dict3.items():
    print("得到的%s有%d個" %(name,num))

print(dict3.items())
```

    得到的金牌有90個
    得到的銀牌有80個
    得到的銅牌有70個
    得到的鐵牌有100個
    得到的金牌有90個
    得到的銀牌有80個
    得到的銅牌有70個
    得到的鐵牌有100個
    dict_items([('金牌', 90), ('銀牌', 80), ('銅牌', 70), ('鐵牌', 100)])
    


```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()
    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
    請輸入此單辭的拼寫: computer
    正確！
    定義: a large body of saltwater that covers most of the Earth's surface
    請輸入此單辭的拼寫: 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
    請輸入此單辭的拼寫: mountain
    正確！
    練習結束。您的得分是 6/6。
    

## 【afterclass practice】
