Skip to content

Instantly share code, notes, and snippets.

@noman-land
Created July 26, 2023 04:00
Show Gist options
  • Save noman-land/c141617c09060f886f36ac0df632c6eb to your computer and use it in GitHub Desktop.
Save noman-land/c141617c09060f886f36ac0df632c6eb to your computer and use it in GitHub Desktop.
Python generators quiz
# 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