Syntax error on my one-liner code

def limit_test(max_char): loop = 1; while not loop > max_char: print(".", end=""); loop += 1

Yes, it’s not possible to write the code this way. ; can only separate “simple” statements, not ones that require a block.

If code like this were allowed, it would be ambiguous: should the loop +=1 be inside the loop, or outside? Of course you intend it to be inside, but this requires common sense and reasoning - not tools that are accessible to a compiler, which must be absolutely consistent, rule-driven and predictable.

“Put all the code on one line” is not a useful or meaningful goal; it’s a hobbyist’s special challenge. Code is supposed to be written to be understandable, as it will be read far more often than it is written (in particular, by you as the author).

1 Like

I’m using the Python interpreter and the one-liner is a lot more convenient to do.

Occasionally, but really for anything beyond the most trivial it is more
convenient to just write a tiny file (pick your own name) and
run it:


Then you can have the file in an editor adjacent to your command line
window, and write nice indented multiline code.

You might be interested in this thread:

in particular this suggestion from it by Jon Crall, which is this form:

 python -c "if 1:
     import pathlib
     for _ in range(10):
         print('this is a demo, bear with me')

By putting an if 1: on the first line, you can indent all the
following lines nicely on the command line.

Cameron Simpson