Python Forum

Full Version: return out of loops syntax error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
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.
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?
(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.
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.
(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.
(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.