開始使用免費開始

走訪巢狀字典

你正要撰寫一個程式,走訪下列的巢狀字典,判斷每道菜應該是冷食還是熱食。

my_menu = {
  'sushi' : {
    'price' : 19.25,
    'best_served' : 'cold'
  },
  'paella' : {
    'price' : 15,
    'best_served' : 'hot'
  },
  'samosa' : {
    'price' : 14,
    'best_served' : 'hot'
  },
  'gazpacho' : {
    'price' : 8,
    'best_served' : 'cold'
  }
}

你能完成這個程式,讓它輸出以下內容嗎?

Sushi is best served cold.
Paella is best served hot.
Samosa is best served hot.
Gazpacho is best served cold.

本練習屬於課程

Data Structures and Algorithms in Python

檢視課程

練習說明

  • 走訪 menu 的各個元素。
  • 列印每道菜應該是冷食還是熱食。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Iterate the elements of the menu
for dish, values in ____.____():
  # Print whether the dish must be served cold or hot
  print(f"{____.title()} is best served {values['____']}.")
編輯並執行程式碼