In C, one can do
while( (i=a) != b ) { }
but in Python, it appears, one cannot.
while (i = sys.stdin.read(1)) != "\n":
generates
while (i = sys.stdin.read(1)) != "\n":
^
SyntaxError: invalid syntax
(the ^
should be on the =
)
Is there a workaround?
Use break:
A version without
functools
:You can accomplish this using the built-in function
iter()
using the two-argument call method:Documentation for this:
Personally I like imm's and Marks answers using
break
, but you could also do:though I wouldn't recommend it.