title: how can I detect infinite loops in python
body: <p>I am learning Python 3 and working on an exercise that calls for writing a Python program which simulates/reads a BASIC program as input. I am stuck on writing the part of the Python program that should detect infinite loops.  Here is the code I have so far:</p>
  
  <pre><code>def execute(prog):
     while True:
        location = 0
        if prog[location] == len(prog) - 1:
           break
           return "success"
        getT = prog[location].split()
        T = len(getT) - 1
        location = findLine(prog, T)
     visited = [False] * len(prog)
  </code></pre>
  
  <p>Here, prog is a list of strings containing the BASIC program (strings are in the form of 5 GOTO 30, 10 GOTO 20, etc.). </p>
  
  <p>T is the target string indicated in prog[location].</p>
  
  <p>If the BASIC program has an infinite loop, then my Python program will have an infinite loop.  I know that if any line is visited twice, then it loops forever, and my program should return "infinite loop". </p>
  
  <p>A hint given by the tutorial assistant says "initialize a list visited = [False] * len(prog) and change visited[i] to True when prog[i] is visited.  Each time through the loop, one value updates in visited[].  Think about how you change a single value in a list.  Then think about how you identify which value in visited[] needs to change."  </p>
  
  <p>So this is the part I am stuck on.  How do I keep track of which strings in prog have been visited/looped through?    </p>