Assignment Condition in Python While Loop
问题 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? 回答1: Use break: while True: i = sys.stdin.read(1) if i == "\n": break # etc... 回答2: You can accomplish this using the built-in function iter() using the two-argument call method: import functools for i in iter(fuctools.partial(sys.stdin.read, 1), '