Python Forum
return out of loops syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return out of loops syntax error
#1
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?
buran write Nov-02-2020, 07:33 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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?
Reply
#5
(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.
Reply
#6
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.
buran likes this post
Reply
#7
(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.
Reply
#8
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,188 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 388 Jan-19-2024, 01:20 PM
Last Post: rob101
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 2,079 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Syntax error while executing the Python code in Linux DivAsh 8 1,608 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,232 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,319 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,261 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 896 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,854 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,365 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020