Teacher's Guide
Chapter 1 / Level 2: Manually advancing a Generator
Teaching Objective:
- Student learns how to work with more complex Generators
- Understands how to manually advance the generator instead of it cycling automatically
- Learns more use cases and applications for generators
Guide
In this exercise you will manually yield the values inside a generator in order to put together ingredients for the refinement of bread. In order to perform these manual calls, we can use the next() function when calling the generator to run through each yield, one at a time, in a sequence.
First set up a reference to the incomplete generator named bread() that’s in the code editor and call it recipe. Walk to the light X mark next to the milk tank and use the collect() function with the next() function in order to yield the first value inside the generator.
recipe = bread()
.......
player.collect(next(recipe))
.......
player.speak(sign)
After you collect the "milk" using the generator, go to the gold X mark next to the sign and use the speak() function in order to acquire the numbers missing for the generator. To do this use the constant sign which holds the message written in the sign itself. Once you read the numbers, fill them out inside the bread() generator.
def bread():
yield "milk"
yield 5
yield "grain"
yield 8
yield "dough"
yield 4
Once the generator is all set up, walk to the X marks inside the facility and use the place() and collect() functions with the next() function in order to go through all the yields inside the generator.
player.place(next(recipe))
...........
player.collect(next(recipe))
...........
player.place(next(recipe))
...........
player.collect(next(recipe))
...........
player.place(next(recipe))
The place() function is used on dark X marks and the collect() function is used on light X marks. Do this on all X marks in order to complete the level.
Map overview and path

Code Solution
import player
def bread():
yield "milk"
yield 5
yield "grain"
yield 8
yield "dough"
yield 4
recipe = bread()
player.turn_right()
player.move_forward(2)
player.turn_left()
player.collect(next(recipe))
player.turn_left()
player.turn_left()
player.move_forward(2)
player.turn_left()
player.speak(sign)
player.turn_right()
player.move_forward(2)
player.place(next(recipe))
player.turn_left()
player.move_forward()
player.turn_right()
player.collect(next(recipe))
player.turn_left()
player.move_forward()
player.turn_right()
player.place(next(recipe))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.collect(next(recipe))
player.turn_left()
player.move_forward()
player.turn_right()
player.place(next(recipe))