How to do variable assignment inside a while(expression) loop in Python?

前端 未结 6 1874
离开以前
离开以前 2020-12-08 22:30

I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.

Here is how I\'m doing it in PH

6条回答
  •  忘掉有多难
    2020-12-08 22:59

    from functools import partial
    
    for name in iter(partial(raw_input, 'Name:'), ''):
        do_something_with(name)
    

    or if you want a list:

    >>> names = list(iter(partial(raw_input, 'Name: '), ''))
    Name: nosklo
    Name: Andreas
    Name: Aaron
    Name: Phil
    Name: 
    >>> names
    ['nosklo', 'Andreas', 'Aaron', 'Phil']
    

提交回复
热议问题