0

I have a python program that I am currently working on which is working pretty well except for the fact that I am running into "recursion depth" issues and I am not sure how to rework my program to avoid this.

my program is basically something like this in a nutshell:

def foo()
    x = 0 # 

    while x != 1:
        x = (mydll.InPortB(base+1)) & 1 # this is a hardware input like a push button(on or off)
        time.sleep(0.5)

     bar = ser.readline() # input from serial source
     if len(bar) != 10:
         foo()
     ''' it continues checking "bar" using some more if statement. Finally when the
         logic is satisfied the program outputs something but still restarts foo() as
         I need the program to run endlessly but I am not sure how to fix the recursive
         nature of the program'''
      if bar = 1234567890:
          print "yay you win"
          foo()
#start
foo()

Ultimately my program works as it is but eventually will crash out with the recursion limit error so not ideal as I need this program to run endlessly but being a total newb to programming I am unsure how to fix it. I did try to split my program into two or three separate function rather than just the one but it was still exhibiting the recursion issue.

Thanks for any input. If anyone needs more code I can post it but thought the small piece should be sufficient to see where I am going. Any help would be appreciated.

6
  • You could make the arming while loop more concise this way: while not (mydll.InPortB(base+1)) & 1: time.sleep(0.5) Commented Mar 21, 2012 at 18:23
  • Am I misreading that, or shouldn't that while block run infinitely if entered? Commented Mar 21, 2012 at 19:00
  • Thanks sr2222. That helped cleanup my code a little. I had tried a few iterations and couldn't get it to work right but your suggestion worked perfectly. Thanks Commented Mar 21, 2012 at 19:18
  • @Daenyth, if you are referring to my original code then "arming" was a misprint, it should have been X instead. Copy/paste issue. :-0 If you were referring to sr2222's suggestion, it worked out perfectly. Commented Mar 21, 2012 at 19:19
  • @ouldsmobile: Your original code. You should update the question with the correct code. Commented Mar 21, 2012 at 19:37

2 Answers 2

1

I would try this since it repeatedly calls the function at the same recursion level:

def foo()
    x = 0 # 
    while arming != 1:
        x = (mydll.InPortB(base+1)) & 1 # this is a hardware input like a push button(on or off)
        time.sleep(0.5)

     bar = ser.readline() # input from serial source
     if len(bar) != 10:

     ''' it continues checking "bar" using some more if statement. Finally when the
         logic is satisfied the program outputs something but still restarts foo() as
         I need the program to run endlessly but I am not sure how to fix the recursive
         nature of the program'''
      if bar = 1234567890:
          print "yay you win"

#start
while 1:
    foo()
Sign up to request clarification or add additional context in comments.

2 Comments

If I infer his logic correctly, I think he may need to pull the if len(bar) != 10: block out and put it in a while loop inside while True: with the print statement coming after the len check on bar in the main body of code.
This is the route I chose to go though I am pretty sure the other answers would have worked as well. I had to rejig a couple of my IF statements but in the end it seems like this is working well. I am going to let it run overnight to see if it runs without exiting. Thanks!!
1

Inside of foo just use a loop such as:

while True:

    x = 0 # 
    while arming != 1:
        x = (mydll.InPortB(base+1)) & 1 # this is a hardware input like a push button(on or off)
        time.sleep(0.5)

     bar = ser.readline() # input from serial source
     if len(bar) != 10:
         # do whatever you want but don't call foo

You don't need to call foo recursively for the program to keep running. The while True will take care of that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.