設計說明:
請撰寫一程式,自行輸入兩個詞典(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),將此兩詞典合併,並根據key值字母由小到大排序輸出,如有重複key值,後輸入的key值將覆蓋前一key值。
輸入輸出:
輸入說明
輸入兩個詞典,直至end結束輸入
輸出說明
合併兩詞典,並根據key值字母由小到大排序輸出,如有重複key值,後輸入的key值將覆蓋前一key值
輸入輸出範例
輸入與輸出會交雜如下,輸出的部份以粗體字表示
Create dict1:
Key: a
Value: apple
Key: b
Value: banana
Key: d
Value: durian
Key: end
Create dict2:
Key: c
Value: cat
Key: e
Value: elephant
Key: end
a: apple
b: banana
c: cat
d: durian
e: elephant
Key: a
Value: apple
Key: b
Value: banana
Key: d
Value: durian
Key: end
Create dict2:
Key: c
Value: cat
Key: e
Value: elephant
Key: end
a: apple
b: banana
c: cat
d: durian
e: elephant
程式執行狀況擷圖
下圖中的 粉紅色點 為 空格

Code:
d1={}
d2={}
print('Create dict1:')
while True:
Key1 = input('Key: ')
if Key1=="end":
break
d1[Key1] = input('Value: ')
print('Create dict2:')
while True:
Key2 = input('Key: ')
if Key2=="end":
break
d2[Key2] = input('Value: ')
d1.update(d2)
for i in sorted(d1.keys()):
print(i,": ",d1[i],sep="")