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

前端 未结 6 1843
离开以前
离开以前 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:48

    You can wrap raw_input() to turn it into a generator:

    def wrapper(s):
        while True:
            result = raw_input(s)
            if result = '': break
            yield result
    
    names = wrapper('Name:')
    

    which means we're back to square one but with more complex code. So if you need to wrap an existing method, you need to use nosklo's approach.

提交回复
热议问题