13

Is it possible to have a Python while loop purely on one line? I've tried this:

while n<1000:if n%3==0 or n%5==0:rn+=n

But it produces an error message: Invalid Syntax at the if statement.

6
  • 1
    Why don't you try it? That is a infinite loop, but it'll work as far as Python is concerned.
    – Martijn Pieters
    Mar 3, 2013 at 12:39
  • @MartijnPieters I have, it doesn't work Mar 3, 2013 at 12:40
  • 3
    "it doesn't work" is not a problem description. What doesn't work? What did you expect to happen, what happened instead? What error did you get?
    – Martijn Pieters
    Mar 3, 2013 at 12:41
  • You can't have nested control structures on one line.
    – Volatility
    Mar 3, 2013 at 12:45
  • 1
    Right, that doesn't work. Complex statements need new lines.
    – Martijn Pieters
    Mar 3, 2013 at 12:45

3 Answers 3

25

When using a compound statement in python (statements that need a suite, an indented block), and that block contains only simple statements, you can remove the newline, and separate the simple statements with ; semicolons.

However, that does not support compound statements.

So:

if expression: print "something"

works, and so does using multiple simple statements:

while expression: print("something"); print("something else")

but:

while expression: if expression: print "something"

does not because both the while and if statements are compound.

For your specific example, you can replace the if expression: assignment part with a conditional expression, so by using an expression instead of a complex statement:

while expression: target = true_expression if test_expression else false_expression

in general, or while n<1000: rn += n if not (n % 3 and n % 5) else 0 specifically.

From a style perspective, you generally want to leave that one line on it's own, though.

7
  • Slightly related, is there any statement equivalent to ?: C statement in Python ? IF there is, it should do the job.
    – asheeshr
    Mar 3, 2013 at 12:49
  • 2
    @AshRj there is, see my answer.
    – l4mpi
    Mar 3, 2013 at 12:49
  • @AshRj: Yes, the conditional expression true_expression if expression else false_expression.
    – Martijn Pieters
    Mar 3, 2013 at 12:50
  • Ten years too late, I know, but those are colons, not semicolons. Mar 2 at 4:40
  • @DawoodibnKareem: no, I'm talking about ; semicolons here, not : colons, when separating simple statements. E.g. you can do print('foo'); print('bar') in Python, because calling an expression statement is a simple statement. I also talk about compound statements, and these use : colons.
    – Martijn Pieters
    Mar 2 at 18:58
5

In your example, you try to collapse two levels of blocks / indentation into a single line, which is not allowed. You can only do this with simple statements, not loops, if statements, function definitions etc. That said, for your example there is a workaround using the ternary operator:

while n < 1000: rn += n if (n % 3 == 0 or n % 5 == 0) else 0

which reads as 'add n to rn if the condition holds, else add 0'.

1

It is posible to do something similar:

rn = 100
for n in range(10): rn += n if (n%3==0 or n%5==0) else 0
1
  • An explanation would be in order. What is the gist? Please respond by editing (changing) your answer, not here in comments (but ******************** without ******************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). Feb 4 at 0:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.