Teacher's Guide
Chapter 1 / Level 3: Sending data to a Generator
Teaching Objective:
- Student learns how to make the most out of Generators
- Understands how to add and process data inside the Generator
- Learns more use cases and operators you can employ with Generators
Guide
In this exercise we must shear the wool of sheep and use it to spin yarn by feeding values into a generator. We can achieve this by using the generator function send() to modify the output the generator produces.
The generator set up for spinning the yarn is named spin() and it is designed to take in an external value and multiply it’s contents with each cycle.
def spin():
cycle = 1
while True:
cycle += 1
yarn = yield
yarn = yarn * cycle
yield player.speak("You've spun %d ft of yarn" % (yarn))
First, walk to the light X marks , face the sheep and use the speak() function together with the dictionary constant named sheep, in order to shear the sheep. To do this you must enter the color of the carpet in front of the sheep as a key to access the correct value from the dictionary.
There are four colors total: "green" , "red" , "blue" and "orange" , the message output will provide you with the number of pounds of wool that have been sheared from the sheep.
player.speak(sheep["green"])
...........
player.speak(sheep["red"])
...........
player.speak(sheep["blue"])
...........
player.speak(sheep["orange"])
Once the sheep have been sheared, set a reference to the generator, head to the dark X marks and face the spindles. Use the next() function to manually access each cycle in the generator then send() the number of pounds of wool associated with each color carpet to the generator.
spindle = spin()
next(spindle)
spindle.send( 6)
...........
next(spindle)
spindle.send( 2)
...........
next(spindle)
spindle.send( 3)
...........
next(spindle)
spindle.send( 4)
The dark X marks will also have colored carpets under them, use the values associated with each color with the send() function in order to complete the level.
Map overview and path

Code Solution
import player
def spin():
cycle = 1
while True:
cycle += 1
yarn = yield
yarn = yarn * cycle
yield player.speak("You've spun %d ft of yarn" % (yarn))
player.turn_left()
player.turn_left()
player.move_forward(8)
player.speak(sheep["green"])
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak(sheep["red"])
player.turn_left()
player.move_forward(4)
player.turn_right()
player.speak(sheep["blue"])
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak(sheep["orange"])
player.turn_left()
player.turn_left()
player.move_forward(4)
spindle = spin()
next(spindle)
spindle.send( 6)
player.turn_left()
player.move_forward()
player.turn_right()
next(spindle)
spindle.send( 2)
player.turn_left()
player.move_forward(2)
player.turn_right()
next(spindle)
spindle.send( 3)
player.turn_left()
player.move_forward()
player.turn_right()
next(spindle)
spindle.send( 4)