import locale and print range on same line

Vlastimil Brom vlastimil.brom at gmail.com
Sat Jan 23 06:12:41 EST 2016


2016-01-23 11:36 GMT+01:00 Marko Rauhamaa <marko at pacujo.net>:
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
if you realy can't use multiple lines (which is rather essencial for
python), you can circumvent this in some cases, e.g. using list
comprehension and ignoring the created list itself:
import locale; locale.setlocale(locale.LC_ALL, "");
[print(locale.format("%2f", i, 1)) for i in range(1,20,4)]

vbr



More information about the Python-list mailing list