Get startedGet started for free

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.

This exercise is part of the course

Python for MATLAB Users

View Course

Exercise 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

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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(____)
Edit and Run Code