avatar Bite 93. Rock-paper-scissors and generator's send

Generators are cool right?! Did you know you can send values into a generator by calling its send(value) method? Crazy right?

Let's do a little experiment: complete Rock-paper-scissors below, where player_choice receives the next value via send, if q is entered raise a StopIteration.

So the game would work something like this (see all scenarios in the tests):

>>> from rps import game
>>> game = game()
>>> next(game)
Welcome to Rock Paper Scissors
>>> game.send('rock')
paper beats rock, you lose!
>>> game.send('paper')
paper beats rock, you win!
>>> game.send('rock')
tie!
>>> game.send('q')
Traceback (most recent call last):
...
StopIteration

Pretty cool no? As this is an Advanced Bite we're not giving too much away. Good luck, have fun and keep calm and code in Python!

Login and get coding
go back Advanced level
Bitecoin 4X

98 out of 98 users completed this Bite.
Will you be the 99th person to crack this Bite?
Resolution time: ~58 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 5.38 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help