Solved

Stage 2 — finding the nearest

OliviaPyOP

How do I track the nearest waypoint while looping?

11

Comments 4

Log in to join the discussion.

Log in Create account
  1. AmeliaRuns Solutionreplies: 2

    Keep two variables before the loop: the best name and the best distance (start with None or infinity). In the loop, calculate the distance once, print it, and update the best if it's smaller.

    6
    1. OliviaPyOPreplies: 1

      float("inf") as the starting distance — neat trick.

      1
      1. ZosiaN

        Right, min with key would call the function again. Good catch.

        2
    2. JackStack

      min() with a key could do it too, but the challenge wants one call per waypoint, so the loop is better.

      5