美文网首页
python 同时使用 yield 的输入和输出

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

作者: 猿来是八阿哥 | 来源:发表于2019-07-29 16:47 被阅读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'}

相关文章

网友评论

      本文标题:python 同时使用 yield 的输入和输出

      本文链接:https://www.haomeiwen.com/subject/iktirctx.html