when you iterate every coin in order to find the closest one, you first have to check every coin toget the closest one as the list isn’t sorted by distance to the hero, otherwise you’d be able to take the first entry of that list all the time.
If you have it while “iterating” through, you’re pretty much always getting the first in the array, while it might not even be the closest coin.
once you then reached the closest coin, it would iterate all the others, but not move to it.
The order in which you do things is important, as with the second while loop you want to get the closest coin.
For example (distance wise) the array you have could be (distance values entered)
Then you iterate through that array and this would be the result:
14 closer than 9001? Yep, so move to it.
17 closer than 14? Nope, ignore it.
32 closer than 14? Nope, ignore it.
4 closer than 14? Yes! But you just moved exactly 14 steps away from it. so it checked if 18 is closer than 14, so you ignore the actual closest coin from starting position.
22 closer than 4? Nope, ignore it.
19 closer than 4? Nope, ignore it.
(One of the others might be closer than 14 now though, so possible that you pick up two coins or more, depending on where they actually are and how you moved during the iteration)
But if you get the if block matching the while true loop, you’re effectively going through all entries in the array and get the closest one.
After you got the one closest to you, you move to it, as it then would be the one with distance of 4 to the hero, instead of the first entry with distance 14.
Additionally, it might happen that you pick up a coin while moving to another, this would then possibly cause an “error” in which you move to a position that you picked up a coin already if you don’t have it in the while True loop, as the array isn’t “refreshed”.