[Tutor] mutual recursion

Jonathan Soons jsoons@juilliard.edu
Tue, 27 Aug 2002 19:53:19 -0400


I am trying to create two lists. I have two functions that call each =
other.
I thought they might act as a flip-flop and add alternately to the two =
lists.
The file that gets read is something like:

PER-BEGIN
blah
blah
blah
AWD-BEGIN
blah
blah
blah
blah
PER-BEGIN
blah
blah
blah
AWD-BEGIN
blah
blah

The script is:

import string, sys, os, time
f =3D open(sys.argv[1], 'r')
txt =3D f.readlines()
f.close()
n =3D 0
awd =3D []
lets =3D []
def incrper() :
        global n, txt, awd, lets
        while txt[n] !=3D "AWD-BEGIN\n" :
                lets.append(txt[n])
                n =3D n + 1
                if n > len(txt) :
                        break
        incrawd()
def incrawd() :
        global n, txt, awd, lets
        while txt[n] !=3D "PER-BEGIN\n" :
                awd.append(txt[n])
                n =3D n + 1
                if n > len(txt) :
                        break
        incrper()
incrper()

The error is:

Traceback (most recent call last):
  File "F:\printawards.py", line 26, in ?
    incrper()
  File "F:\printawards.py", line 10, in incrper
    while txt[n] !=3D "AWD-BEGIN\n" :
IndexError: list index out of range


Is there a right way to do this?

Thanks