Python Forum
return out of loops syntax error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: return out of loops syntax error (/thread-30712.html)



return out of loops syntax error - felixf - Nov-02-2020

Hello good people

this is my python code:

if (isSubset(onesArray, twosArray, len(onesArray), len(twosArray))):
    if (twosArray == onesArray[:len(twosArray)]):
      print("The data structure is a queue.")
      return True
When i run this, I get a syntax error that says

Error:
SyntaxError: 'return' outside function
I tried to replace 'return' with 'break', but that did not seem to help as well.

Can I know what i am doing wrong here?


RE: return out of loops syntax error - jefsummers - Nov-02-2020

Please repost the code using the python tags - click the blue and yellow icon on the editor tool bar and put the code in between.
Indentation is then preserved, which probably matters overall, but you are using return when you are not in a function. A function starts with def


RE: return out of loops syntax error - buran - Nov-02-2020

there is no function in your code, thus the error. Nor there is loop, so break will not work either. But you don't need any of them anyway. what you get is just 2 nested if statements.


RE: return out of loops syntax error - GOTO10 - Nov-02-2020

Return is used to exit a function (if used with no return value) or to return a value when the function is called. Similarly, break is used to exit out of a loop. You have neither in your code, so neither is appropriate.

What is it you want to happen once the expression you are evaluating is determined to be true?


RE: return out of loops syntax error - felixf - Nov-03-2020

(Nov-02-2020, 07:38 PM)GOTO10 Wrote: Return is used to exit a function (if used with no return value) or to return a value when the function is called. Similarly, break is used to exit out of a loop. You have neither in your code, so neither is appropriate.

What is it you want to happen once the expression you are evaluating is determined to be true?


i want the code to stop there and not check for any more conditions.


RE: return out of loops syntax error - deanhystad - Nov-03-2020

You can return out of a function. You can break out of a loop. You can exit out of a program. You can also organize your logic such that none of those are necessary.

Your tiny snippet of code does not provide any guidance on which way is the right way. As is, your code does stop right after the print and doesn't check any more conditions, or do anything for that matter. There is nothing left to do.


RE: return out of loops syntax error - GOTO10 - Nov-03-2020

(Nov-03-2020, 04:15 AM)felixf Wrote:
(Nov-02-2020, 07:38 PM)GOTO10 Wrote: What is it you want to happen once the expression you are evaluating is determined to be true?
i want the code to stop there and not check for any more conditions.

As deanhystad indicated, the code snippet you provided ends after the conditional check regardless of its outcome. If you're still not sure how to accomplish your larger purpose, you need to show us more code to get further guidance.


RE: return out of loops syntax error - perfringo - Nov-03-2020

(Nov-02-2020, 07:20 PM)felixf Wrote:
Error:
SyntaxError: 'return' outside function
I tried to replace 'return' with 'break', but that did not seem to help as well.

Can I know what i am doing wrong here?

return may only occur syntactically nested in a function definition.

For more information: Documentation > The Python Language Reference > 7. Simple statements > 7.6. The return statement.

break may only occur syntactically nested in a for or while loop

For more information: Documentation > The Python Language Reference > 7. Simple statements > 7.9. The break statement

As you have no function defined nor for- or while-loop therefore neither return or break is allowed.