【程序分析】
● 字典中的條目是沒有順序的。
● 可以對字典使用如下方法: keys()、values()、 items()、 clear()、 get(key)、 pop(key) 和popitem()
【程序代碼】
dictionary={"dog":"狗","apple":"蘋果","banana":"香蕉","cat":"貓","peach":"桃子"
}def find(word):return dictionary.get(word, "Sorry,no finding.")while True:x=input("Please input a word (input q to exit):")if x=="q":breakmeaning=find(x)#print(f"{x}'s meaning:{meaning}")print("{}'s meaning:{}".format(x,meaning))
【程序輸出】
Please input a word (input q to exit):dog
dog's meaning:狗
Please input a word (input q to exit):cat
cat's meaning:貓
Please input a word (input q to exit):op
op's meaning:Sorry,no finding.
Please input a word (input q to exit):q
>>>
?