title: turn my for loop into a function in python
body: <p>I would like to know how can I turn my <code>for</code> loop into a function.</p>
  
  <p>I wrote a code which asks the user to input an address, then the code iterate through preset addresses and return me the closest address to the input one.</p>
  
  <p>I would like to make it a function so I can call it in my GUI.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import urllib.request
  import json
  
  # Google Maps directions API endpoint
  endpoint = 'https://maps.googleapis.com/maps/api/directions/json?'
  api_key = 'MY API KEY'
  
  # donner l'adresse en code postal
  origin = input("Inserer l'adresse du fabricant : ").replace(' ', '+')
  destinations = ['H7N 5H9', 'H4N 1P9', 'K4A 0G2']
  distances = []
  
  # Parcourt la liste des adresses et calcule chacune de leurs distances
  def proximity():
      for i in range(len(destinations)):
          currentDestination = destinations[i].replace(' ', '+')
          nav_request = 'origin={}&amp;destination={}&amp;key={}'.format(origin, currentDestination, api_key)
          request = endpoint + nav_request
          response = urllib.request.urlopen(request).read()
          directions = json.loads(response)
          distance = directions["routes"][0]["legs"][0]["distance"]["text"]
          distances.append(distance)
          closestDistance = min(distances)
          return closestDistance
  
  print("the distance is: " + proximity())
  print("closest address is : " + destinations[distances.index(proximity())])
  </code></pre>
  
  <p>EDIT :</p>
  
  <p>i'm not getting the error below anymore though </p>
  
  <p>I'm getting this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/cn/PycharmProjects/Calendrier/venv/proximity.py", line 25, in &lt;module&gt;
      print("the distance is: " + closestDistance)
  NameError: name 'closestDistance' is not defined
  </code></pre>
  
  <p>What I want, is to be able to call this function in my other module. 
  The code works perfectly with the for loop, I just want to be able to transform it into a function.</p>