TQC+ 程式語言 Python 3 _ 709 詞典排序

說明:
請撰寫一程式,輸入一顏色詞典color_dict(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),再根據key值的字母由小到大排序並輸出。

輸入與輸出會交雜如下,輸出的部份以粗體字表示
Key: Green Yellow
Value: #ADFF2F
Key: Snow
Value: #FFFAFA
Key: Gold
Value: #FFD700
Key: Red
Value: #FF0000
Key: White
Value: #FFFFFF
Key: Green
Value: #008000
Key: Black
Value: #000000
Key: end
Black: #000000
Gold: #FFD700
Green: #008000
Green Yellow: #ADFF2F
Red: #FF0000
Snow: #FFFAFA
White: #FFFFFF


程式碼:
color_dict = {}

dictKey = input('Key: ')
while dictKey != "end":
    dictValue = input('Value: ')
    color_dict[dictKey] = dictValue
    dictKey = input('Key: ')

for i in sorted(color_dict.keys()): print(i,": ",color_dict[i],sep='')

沒有留言:

張貼留言