LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Python for MATLAB Users

Kurs anzeigen

Anleitung zur Übung

  • 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

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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(____)
Code bearbeiten und ausführen