3

I have a problem with the next code.

try: 1+1
except Exception as exception: pass
1+1
try: 2+2
except Exception as exception: pass

The result I get in the prompt is

... ... File "<stdin>", line 3
 1+1
 ^
SyntaxError: invalid syntax
>>> ... ... ... 4

However the next code executes with no error.

try: 1+1
except Exception as exception: pass
try: 2+2
except Exception as exception: pass

My sys.version_info is:

sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)

Why do I get the syntax error?

asked Feb 16, 2015 at 19:29

1 Answer 1

5

When using the interactive prompt, there needs to be a blank line between a block (such as a try/except block) and the next independent command. This is only in the REPL, when running a .py file it's not necessary.

answered Feb 16, 2015 at 19:32
Sign up to request clarification or add additional context in comments.

1 Comment

@Usobi basically, when you're in the REPL and the prompt is ... instead of >>>, you're still inside a block.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.