Re:Python: Uncovering the Overlooked Core Functionalities
by noman-land on Tuesday July 25, 2023 @12:53AM
(#36858045)
I am a python noob and this is going to take me some time to process.
[ Parent
| on HN ]
Re:Python: Uncovering the Overlooked Core Functionalities
by contravariant on Tuesday July 25, 2023 @05:35AM
(#36859971)
Best way to think about it is that a generator can throw some questions back to the caller. It always looks a bit messy though.
question_bank={'1+1' : '2', '2+3' : '5'}
def Quiz():
for question, correct_answer in question_bank.items():
answer = yield question
if answer == correct_answer:
print('Correct!')
else:
print('Wrong.')
yield 'Finished!'
question = Quiz()
q = next(question)
while q != 'Finished!':
q = question.send(input(q))