3 ms·
The one-line shell command python version ended up being surprisingly painful: echo "# -*- coding: utf-8 -*-\\nfrom random import choice\\nwhile(True):\\n
by lloeki 14y ago
The one-line shell command python version ended up being surprisingly painful:
echo "# -*- coding: utf-8 -*-\\nfrom random import choice\\nwhile(True):\\n print(choice(\"╱╲\"), end='')" | python3
or
python3 -c 'exec("from random import choice\nwhile(True):\n print(choice(\"╱╲\"), end='"''"')")'
(Oh, the fun of deeply nested quote escaping!)
The exec/echo thing being required since we need multiple lines, because while ';' is the statement separator python barfs on "import foo; while (True): pass". Bug or spec?
- lifthrasiir 14y agoIt is a spec. Python in general prefers a statement over an expression, so the while statement does not have an expression form.
- dalke 14y agoPython has two types of statements; "simple" statement and "compound" statements. Compound statements are those, like the if- and while-statements, which require multiple lines. Only simple statements may be separated by a ';', not compound statements. This is a bit different than what you wrote. For example, "x=4" is a statement, and not an expression, but "x=4;y=5;z=6" is a valid Python line because assignment is a simple statement, and multiple simple statements may be separated by a ';'.
- anonymous 14y agoI did it with python3 -c "`echo -e 'import time\nwhile True:\n print(u"\u2571\u2572"[int(time.time() * 100)%2],end="")\n time.sleep(0.001)'`" The neat thing about it is that the randomness comes from the inexactness of time.sleep
- ionelm 14y agoYou can write it like this: python -c "while 1:import sys,random;sys.stdout.write(random.choice('\/'))" and it's shorter too !