by shigemk2


エキスパートPythonプログラミング send


#-*- coding:utf-8 -*-"
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("A that's good, go on")
            elif 'bad' in answer:
                print("Don't be so negative")

free = psychologist()
print next(free) # # Please tell me your problems
# None
# sendを使うとyieldが渡された値を返す。
free.send('I feel bad') # Don't be so negative
free.send("Why shouldn't?") # Don't ask yourself too much questions
free.send("ok then i should find what is good for me") # A that's good, go on