import locale and print range on same line

Marko Rauhamaa marko at pacujo.net
Sat Jan 23 05:36:18 EST 2016


raiwil at gmail.com:

> Can someone tell me why next code doesn't work?
>
> import locale; locale.setlocale(locale.LC_ALL, ""); for i in
> range(1,20,4): print(locale.format("%2f", i, 1))
>
> It gives an error: SyntaxError: invalid syntax --> indicating 'for'
>
> However I need to put the code on one single line.
> When I separate them like below it works fine.
>
> import locale
> locale.setlocale(locale.LC_ALL, "")
> for i in range(1,20,4):
>    print(locale.format("%2f", i, 1))

The answer is in Python's syntax definition. Not everything is allowed
on a single line.

See <URL: https://docs.python.org/3/reference/grammar.html>

Only small_stmt's can be separated by semicolons.

A for statement is a compound_stmt, which is not a small_stmt.


Marko



More information about the Python-list mailing list