帮忙看下这段generator代码

握草 握草 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
def psychologist():
print('Please tell me your problems')
while True:
answer = yield
if answer is not None:
if answer.endswith('?'):
print('Don\'t ask yourself too much questions...')
elif 'good' in answer:
print('That\'t good. Go on.')
elif 'bad' in answer:
print('Be positive. Believe in yourself.')
else:
print('Fuck you. What are you talking about!')


谁能解释下下面的输出?

>>> free = psychologist()
>>> next(free)
Please tell me your problems
>>> free.send('how are you?')
Don't ask yourself too much questions
>>> next(free)
>>> next(free)
>>> next(free)
>>> next(free)

为什么只有第一次在next(free)后面出现那行:Please tell me your problems
而后面不管怎么next(free),都没有任何作用(是真的没有任何作用么?)
求解。
...全文
评论 7
收藏 已收藏
分享
返回顶部
握草
脚本语言 37,738 成员 34,208 内容