IniziaInizia gratis

Iterating over a nested dictionary

You are writing a program that iterates over the following nested dictionary to determine if the dishes need to be served cold or hot.

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'
  }
}

Can you complete the program so that it outputs the following?

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

Questo esercizio fa parte del corso

Data Structures and Algorithms in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Iterate over the elements of the menu.
  • Print whether the dish must be served cold or hot.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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['____']}.")
Modifica ed esegui il codice