0
 def foo():
     l = [0, 0, 0]
     for i in range(3):
         l[i] = random.random()
         for j in l:
            if j > 0.5:
                for i in range(3):
                    l[i] = random.random()
                foo()
            else:
               print("Returning...")
               return None
      return None

My recursive function modifies a list l. The recursive call is nested while iterating on l. The problem is that when returning from a nested call, the list on which the for loop is iterating will be a different one (because l is overwritten during the nested call).

Example: l = [0.7, 0.5, ...] at the first execution of foo(), then j = 0.7; let us assume that l becomes l = [0.3, 0.86, ...] in the first nested call of foo(); when foo() returns, I need j to take the value 0.5, but that does not happen because l has been overwritten in the meantime.

So, how can I use (modify) the list l in my recursive function and, at the same time, make sure that once an iteration begins on l, it is done on the same list until its end?

5
  • sorry....in the for loop, the call is foo(), not foo(count) Commented May 16, 2017 at 1:12
  • You can edit your post and remove the count The edit link is just below the post text. Commented May 16, 2017 at 1:16
  • You are not modifying the list in the recursion. Commented May 16, 2017 at 1:21
  • 1
    It's hard to correct your logic when we have no idea what this function is supposed to do. Commented May 16, 2017 at 1:25
  • we need a canonical "why does my recursive function not work?" – I see people write recursive calls without return almost every single day Commented May 16, 2017 at 1:58

2 Answers 2

1

Like other commentators have noted, it's hard to give specific advice without more information. However, passed on the key points of your question, I'd say you need to look into passing the list l into each recursive call as a parameter, and then return each modification as the result of foo. This was, each recursive call can work on the "same" list throughout the computation, and finally return the answer at the top level.

Sign up to request clarification or add additional context in comments.

Comments

1

It's hard to correct your logic when we have no idea what this function is supposed to do. Let's annotate what the function does, and hope that you can see your logical error:

def foo():
     # Create a local list l
     l = [0, 0, 0]

     # Ignore those initial zeroes;
     #   replace each in turn with a random number
     for i in range(3):
         l[i] = random.random()

         # Grab the next number in the list;
         #   this is the one we just generated.
         for j in l:
            # If that number is greater than 1/2,
            #   rewrite the entire local list.
            # Then recur on foo.
            if j > 0.5:
                for i in range(3):
                    l[i] = random.random()
                foo()
            # If any element of the rewritten list is less than 1/2,
            #   return now.
            else:
               print("Returning...")
               return None
      return None

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.