s = """ Q: What is a good way of achieving this? A: I am not sure. Try the following: 1. Take this first step. Execute everything. 2. Then, do the second step 3. And finally, do the last one Q: What is another way of achieving this? A: I am not sure. Try the following alternatives: 1. Take this first step from before. Execute everything. 2. Then, don't do the second step 3. Do the last one and then execute the above step """ def CoRoutine(cr): def Wrapper(*args, **kwargs): result = cr(*args, **kwargs) next(result) return result return Wrapper @CoRoutine def QA(out): def Send(question, answer_arr): out.send((question, '\n'.join(answer_arr))) question = None while True: while True: try: line = (yield) except GeneratorExit: Send(question, answer_so_far) raise moar_answer, is_question, next_question = line.partition(' Q: ') if is_question: if question: Send(question, answer_so_far) question = next_question break elif question: answer_so_far.append(moar_answer) while True: line = (yield) _, is_answer, answer = line.partition(' A: ') if is_answer: break answer_so_far = [answer] @CoRoutine def Printer(): while True: stuff = (yield) print stuff out = Printer() qa = QA(out) for line in s.split('\n'): qa.send(line)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question