2

I'm assuming this is going to be stupidly easy to answer, yet I've searched all over and can't find an answer. I'm learning Python and trying to run some very simple code, but I keep getting a syntax error any time I try to do something after an indented block. For example:

x = [1,2,3];
for i in x:
    print(i);
print('finished');

When I run this code, I get a syntax error on the print('finished') part. Any time I try to run anything by unindenting after a block like a loop or if statement, I get this error. I'm running Python 3.2.3 in IDLE on Mac OS X Lion.

UPDATE: seems this wasn't as easy as I thought and maybe I'm trying to get something to work that is pointless. I guess the shell only runs multiline statements when you're running a block that indents, but the moment you get back to the top level it executes the statements. Since I'll usually be working with files, most likely in Django, won't matter in the end. Thanks for all the amazingly fast responses though.

2
  • 4
    Not your problem, but Python doesn't need semicolons to end lines. Commented May 31, 2012 at 21:28
  • See my answer: use an IDLE edit window. Commented May 31, 2012 at 22:13

4 Answers 4

3

At least the python interactive interpreter on my Ubuntu system requires a newline to end the block:

>>> x = [1,2,3];
>>> for i in x:
...     print(i);
... print('finished');
  File "<stdin>", line 3
    print('finished');
        ^
SyntaxError: invalid syntax
>>> x = [1,2,3];
>>> for i in x:
...     print(i);
... 
1
2
3
>>> print('finished');
finished

Funny enough, the python interpreter does not require the blank line when run on scripts:

$ cat broken.py 
#!/usr/bin/python

x = [1,2,3];
for i in x:
    print(i);
print('finished');

$ ./broken.py 
1
2
3
finished
$ 
Sign up to request clarification or add additional context in comments.

5 Comments

The language itself doesn't require a newline, but the basic interactive interpreter can't figure out that an indented block has ended without one, and same for IDLE. (Some other IDEs may be a bit smarter.)
Thing is, how do I add that newline character it wants? If I hit enter inside the loop it goes to another line, but if I unindent and hit enter, it executes the code
@Rocket04: If you are defining a function or method, it'll by necessity wait until the entire function is defined and called before it can execute the code.
@sarnold There's no function at all, the code I'm running is only what I typed in this example. I'm starting to think I'm looking at this the wrong way. The shell may just not be made to run this code, it wants you to run blocks separately. If I want to run multiple blocks, I guess I can run them one after the other or simply use a file/module
Yeah, if you're just fiddling with the thing as a handy REPL source, then it doesn't much matter when it gets around to executing the loop vs executing the statements following the loop. It really only matters when you're defining a function or method for later execution.
1

Take out all your semicolons.

x = [1,2,3]
for i in x:
    print(i)
print('finished')

2 Comments

This is definitely a good suggestion for other reasons, but completely irrelevant to the problem at hand.
The script in my post works on my mac running python 3.2.3 and my windows machine running 3.2.
1

Insert a newline after print(i) (also on windows)

x = [1, 2, 3]
for i in x:
    print(i)

print('finished')

Comments

1

Are you trying to enter this code in IDLE's default Python Shell window? You are better off opening an IDLE editor window (menu item File -> New Window) and running the code from there (menu item Run -> Run Module). Indenting in the shell window can get confusing and it is difficult to correct mistakes.

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.