3

I have the following function:

    def in_loop(i):
        global loop_started
        if i == '[':
            loop_started = True
            return [True, 'loop starting']
        if loop_started:
            if i == ']':
                loop_started = False
                return [True, 'loop over']
            return True
       return False

I believe this is returning a tuple that looks like (True, 'loop over') when i is "]". I then try to index into it with

for index, i in enumerate(code):
    if in_loop(i):
        loop_counter += 1
        if in_loop(i)[1] == 'loop starting':
            loop_start = index
        if in_loop(i)[1] == 'loop over':
            loops[f'loop{loop_num}'] = {'start': loop_start, 'end': index}
            loop_num += 1

but this raises an error

TypeError: 'bool' object is not subscriptable

Also, code = "+++++[-][-]".

Why is this error being raised when I'm indexing into a tuple?

9
  • 1
    What happens when i is not ] too. (None is returned) Commented Mar 27, 2019 at 2:55
  • 1
    @Kingsley I think that's the problem there, in fact, I also believe there's a missing piece of code that return a boolean Commented Mar 27, 2019 at 2:56
  • This works for me: if in_loop(']')[1] == 'loop_over': yet if I don't pass ']' as i, i get NoneType object is not subscriptable Commented Mar 27, 2019 at 2:56
  • When i is not ], it does other stuff, I just didn't include that to make this easier to read Commented Mar 27, 2019 at 2:56
  • The problem is return False / return True when i is neither '[' or ']' Commented Mar 27, 2019 at 3:00

3 Answers 3

2

The problem is that when the characters like '+' or '-' are reached you are essentially returning boolean but are accessing if in_loop(i)[1] == 'loop starting': nonetheless.

You must return a consistent return type for the 2nd for-loop code to work. For ex, look at the comments below to your code:

def in_loop(i):
    global loop_started
    if i == '[':
        loop_started = True
        return [True, 'loop starting']
    if loop_started:
        if i == ']':
            loop_started = False
            return [True, 'loop over']
        return True  #This will have side effects and is inconsistent with your other returns of in_loop
   return False  #This will have side effects and is inconsistent with your other returns of in_loop
Sign up to request clarification or add additional context in comments.

Comments

1

This is the case only when you input something that isn't '[' or ']', because it would got to the second if of if loop_started:, and default if the inner condition doesn't pass, it would just return True, so that is why it doesn't work.

Comments

0

What did you init the var loop_started as? (Or in another word, what value does loop_started have when the function have not been executed?)

If loop_started is False before the function is executed, then the function would return False directly.

A quick fix would be adding an empty string after all boolean return statements.

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.