1

When Python is first installed, the default setting executes users' code input line-by-line. But sometimes I need to write programs that execute multiple lines at once. Is there a setting in Python where I can change the code execution to one block at once?

>>> if (n/2) * 2 == n:;
        print 'Even';
        else: print 'Odd'

Error:

SyntaxError: invalid syntax

When I tried to run the above code, I got an invalid syntax error on the else.

4
  • It looks like you are trying to write an application featuring concurrency. There are a number of ways to do this but the solution really depends on the problem domain. Can you provide some context? Commented May 19, 2010 at 7:20
  • Python programs very nearly never contain semicolons (;) (as part of the language, outside a string). If you're using semicolons, you're probably doing something wrong. Do not use semicolons unless you're absolutely sure you know why you need one. Commented May 19, 2010 at 7:40
  • I guess semicolons in python are only useful when you do code-golf Commented May 19, 2010 at 7:42
  • Ah... that kind of multiple lines at once :-D Commented May 19, 2010 at 12:22

4 Answers 4

9

Your indentation is wrong. Try this:

>>> if (n/2) * 2 == n:
...     print 'Even'
... else: print 'Odd'

Also you might want to write it on four lines:

>>> if (n/2) * 2 == n:
...     print 'Even'
... else:
...     print 'Odd'

Or even just one line:

>>> print 'Even' if (n/2) * 2 == n else 'Odd'
Sign up to request clarification or add additional context in comments.

Comments

1

One step towards the solution is to remove the semicolon after the if:

if True:; print 'true'; print 'not ok'; # syntax error!

if True: print 'true'; print 'ok'; # ok

You cannot have an else in the same line because it would be ambiguous:

if True: print 't'; if True: print 'tt; else: ... # <- which if is that else for??

It is also clearly stated in the docs that you need a DEDENT before the else statement can start.

3 Comments

It's not actually ok at all to use semicolons after any expression;
@hyperboreean: quoting the docs: "A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines."
see my answer for python >=2.5
0

Since python 2.5 you can do one line ifs

print ('Even' if n % 2 == 0 else 'Odd')

Still to answer your question you can either:
1. enter the code properly without syntax errors and your blocks will be executed as blocks regardless if they span multiple lines or not, even in interactive shell. See tutorials in dive into python
2. write code in the script and execute that script using either command line or some IDE (idle, eclipse, etc..)

One of the idea behind python is to prefer multiple lines and to aim for uniform formatting of source, so what you try to do is not pythonic, you should not aim to cram multiple statements into single line unless you have a good reason.

Comments

0
print n % 2 == 0 and 'Even' or 'Odd'

:-)

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.