[Tutor] RE: Tutor digest, Vol 1 #363 - 6 msgs

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Mon, 17 Jul 2000 14:36:22 -0700 (PDT)


> Have you tried making strip recursive?  Change it to look like this:
> 
> def strip(input):
> ____while (len(input) > 0):
> ________if input[0] == ">" or input [0]==" ":
> ____________input = strip(input[1:])
> ____return input
> 
> I didn't have a chance to test it with a file.  I just did a quick
> check in IDLE and it stripped off the leading "> > > >"'s that if fed
> it.  Let me know if that works.

This is dangerous because it runs into a similar problem if the input line
contains something like "||| Another infinite loop |||" or some other
non-numeric characters.  In fact, it only works if your line consists of
nothing but '>' or ' ' characters!  You probably meant to put:


###
def strip(input):
  if input[0] == ">" or input [0]==" ":
    input = strip(input[1:])
  return input
###

instead.  Be careful with mixing recursion with 'while' statement; believe
me, I've had plenty of bad experiences with this bug.