avatar Bite 70. Create your own iterator

In this Bite you make an iterator called EggCreator by implementing the __iter__ and __next__ dunder methods (as dictated by the iterator protocol).

Have it receive an int called limit which is the max number of eggs to create. It then generates eggs of randomly chosen colors from the COLORS constant.

Make sure you raise the right exception when you reach the passed in limit.

So this is how the iterator would work + its output:

for egg in EggCreator(5):
    print(egg)

Would output (colors will vary due to randomness):

green egg
yellow egg
yellow egg
blue egg
red egg

Eggs theme? Yep, this Bite was produced the 31st of March 2018 - Happy Easter all!

Good luck and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #205 to crack this Bite?
Resolution time: ~35 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.43 on a 1-10 difficulty scale.
» You can do it! 😌

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

Ask for Help