title: Recursive function gets stuck in a temporary loop
body: <p>I have a dictionary with names as keys, and a list of the people they've met as values.
  e.g.:</p>
  <pre><code>meetings = {'a': ['b', 'e'], 'b': ['a', 'c', 'e'], 'c': ['b', 'd'], 'd': ['c'], 'e': ['a', 'b']}
  </code></pre>
  <p>Now I'm trying to make a function to check if there's a 'link' between two people (with a maximum distance between these two people if specified). The variable 'distance' is the amount of people inbetween person_X and person_Y. I currently have this:</p>
  <pre><code>def had_contact_with(meetings, person_X, person_Y, distance=-1):
      if person_Y in meetings[person_X] or person_X == person_Y:
          return True
      elif distance == 0:
          return False
      elif distance == -1: #if there is no limit on the distance specified, max distance is len(meetings)
          distance = len(meetings)
      for person in meetings[person_X]:
          if had_contact_with(meetings, person, person_Y, distance-1):
              return True
  </code></pre>
  <p>My code works, but sometimes it gets stuck in a loop for a while. For example if I try:</p>
  <pre><code>print(had_contact_with(meetings,'a','d'))     
  </code></pre>
  <p>It starts checking the contacts of person a, which are b and e. Then it checks person b's contacts, but person a is first in the list of b's contacts, so it just keeps checking between person a and person b, until distance becomes 0. Is there a way to stop it from going in a loop?</p>
  <p>Thanks in advance</p>