始める無料で始める

さまざまな航空機タイプのカウント

この演習では、人気のある商用旅客機について、エンジンの種類を格納した dictionary(辞書)aircraft_engines が与えられています。このデータセットに各エンジンタイプの飛行機が何機あるかを数えたいとします。for ループと if/else 文を使って制御フローを組み立て、新しいエンジンタイプが見つかるたびに counts 変数に 1 を加えてください。

この演習はコースの一部です

MATLABユーザーのためのPython

コースを見る

演習の手順

  • airplane_types の各キーと値のペアをたどる for ループを書いてください。
  • 各反復で、「The x airplane has y engines.」という文を出力します。ここで x は航空機の名前、y はエンジンの種類です。
  • それぞれの反復で、counts 辞書の engine_type キーに対応する値をインクリメントしてください。
  • 最後に、"counts" 辞書を出力してください

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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(____)
コードを編集して実行