python 同时使用 yield 的输入和输出

2019-07-29  本文已影响0人  猿来是八阿哥
# -*- coding: utf-8 -*-
def yield_user_data(properties):
    for property in properties:
        value = yield property

def input_data(properties):
    yud = yield_user_data(properties)
    property = next(yud)
    data = dict()
    while property:
        try:
            value = raw_input('input {0} of data: '.format(property))
            data[property] = value
            property = yud.send(value)
        except StopIteration as si:
            break
    return data

if __name__ == "__main__":
    print 'data you inputed: {0}'.format(str(input_data(['name', 'age', 'sex', 'tel'])))
input name of data: zhangsan
input age of data: 22
input sex of data: M
input tel of data: 13566666666
data you inputed: {'age': '22', 'tel': '13566666666', 'name': 'zhangsan', 'sex': 'M'}
上一篇 下一篇

猜你喜欢

热点阅读