# 9. 輸入資料到字典中,直到輸入鍵值為end結束,再輸入一個鍵值,進行搜尋這個鍵值是否存在字典中。
持續輸入一組一組 key:value 的資料到 字典
輸入的 key 為 end 時結束
再輸入一個 key,查詢它是否存在於字典中
# 2025/04/21-20:45
dict1 = {}
# 1. 輸入 key:value 到字典
print("請輸入 key:value 的資料(key 為 end 結束):")
while True:
key = input("請輸入鍵 key:")
if key == "end":
break
value = input("請輸入對應的值 value:")
dict1[key] = value
# 2. 搜尋某個鍵值是否存在
find_key = input("請輸入要查詢的鍵 key:")
if find_key in dict1:
print(f"✅ 有找到!{find_key} 對應的值是:{dict1[find_key]}")
else:
print(f"❌ 沒有找到鍵值 {find_key}")
請輸入 key:value 的資料(key 為 end 結束):
請輸入鍵 key:apple
請輸入對應的值 value:蘋果
請輸入鍵 key:banana
請輸入對應的值 value:香蕉
請輸入鍵 key:end
請輸入要查詢的鍵 key:apple
✅ 有找到!apple 對應的值是:蘋果
功能 建議使用
字典的建立 dict1 = {}
加入資料 dict1[key] = value
查詢是否存在 if key in dict1:
顯示對應值 dict1[key]
畫線圈重點字(像是「字典」「查詢 key」「最大值」)
轉換成自己理解的句子:「就是要我一直輸入 key 跟 value,然後查一個 key」
試著手寫一組測資,看看應該怎麼出現輸出
列出預期的資料結構 → 這題就應該想到 dict!