Exercise

Controlling loop execution

A typical pattern is to create a while loop with True as its condition and use a break statement to end it. In this exercise, your manager wants you to assemble a list of the five most recent years that the US had a positive trade gap. The dictionary nea is a mapping of datetimes to floats representing the net export for a given year. An empty list named surplus_years and a datetime named query_date are supplied.

Instructions

100 XP
  • Create a loop with a condition that is always true.
  • Skip steps where net exports are less than zero.
  • Check the number of surplus years gathered.
  • Stop the loop once five years have been gathered.