Created
July 26, 2023 04:00
-
-
Save noman-land/c141617c09060f886f36ac0df632c6eb to your computer and use it in GitHub Desktop.
Python generators quiz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified version of https://news.ycombinator.com/item?id=36859971 | |
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('\nCorrect!\n') | |
else: | |
print('\nWrong.\n') | |
yield 'Finished!' | |
question = Quiz() | |
q = next(question) | |
while q != 'Finished!': | |
q = question.send(input(q)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment