CommencerCommencer gratuitement

Counting different types of aircraft

For this exercise, you have a dictionary, aircraft_engines that contains the engine type of a number of popular commercial airplanes. You want to count how many airplanes of each engine type exist in this dataset. Use your Python control flow skills by creating a for loop that contains an if/else statement that adds 1 to the counts variable for every new engine type.

Cet exercice fait partie du cours

Python for MATLAB Users

Afficher le cours

Instructions

  • Write a for loop that goes through each key:value pair of airplane_types.
  • On each iteration, "The x airplane has y engines." should be printed out, where x is the name of the airplane and y is the type of engine.
  • For each iteration, increment the values of the engine_type key in the counts dictionary.
  • Finally, print the "counts" dictionary

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

counts = {}
# Loop over the key:value pairs in airplane_types
for airplane, engine_type in ____.items():
    # Print the aircraft name and engine type of each aircraft
    print("The {} airplane has {} engines.".format(____, ____))
    # Increment the values of the engine_type key in the counts dictionary
    if engine_type in counts:
        counts[engine_type] = ____[engine_type] + 1
    else:
        counts[engine_type] = 1

print(____)
Modifier et exécuter le code