Teacher's Guide
Chapter 8 / Level 5: Custom functions using dictionaries
Teaching Objective:
- Student learns how to use dictionaries with custom functions
- Learns how to integrate dictionaries with loops
- Understands the applications of advanced conditions while using functions with dictionaries
Guide
In this exercise, you need to collect seeds and water to prep for sowing the field, then sow the field by using a custom function manipulating a dictionary.
First walk and grab the bag that holds the seeds, this will grant you the following dictionary: "orange : 3" , "pear : 4" , "apple : 3" , "peach : 2" . Once you have access to this walk to the light X mark, face the well and collect water using the collect() function, like this: player.collect("water') .
Once all resources are gathered, create a function called sow_field that takes the arguments: tree , loops and direction .
def sow_field(tree,loops,direction):
for x in range(loops):
if direction == "right":
player.move_forward(2)
player.turn_left()
player.plant(seeds,tree)
player.water()
player.turn_right()
player.move_forward()
if direction == "left":
player.move_forward()
player.turn_right()
player.plant(seeds,tree)
player.water()
player.turn_left()
player.move_forward(2)
The tree argument specifies the name of the seed to plant, those being: "orange" , "pear" , "apple" and "peach". The loops argument being the amount of times you want the code to loop, this should be equal to the amount of seeds you’re planting of the specific type. The direction argument is what direction you wish your character to move when the loop takes place, be it "left" or "right"
Head to the top of the map, adjacent to the top row of X marks and use the custom function to plant and water the trees. Continue doing this for all four different tree seeds as marked by the signs.
sow_field("orange", 3, "right")
.........
sow_field("pear", 4, "left")
.........
sow_field("apple", 3, "right")
.........
sow_field("peach", 2, "left")
Map overview and path

Code Solution
import player
def sow_field(tree,loops,direction):
for x in range(loops):
if direction == "right":
player.move_forward(2)
player.turn_left()
player.plant(seeds,tree)
player.water()
player.turn_right()
player.move_forward()
if direction == "left":
player.move_forward()
player.turn_right()
player.plant(seeds,tree)
player.water()
player.turn_left()
player.move_forward(2)
player.turn_left()
player.turn_left()
player.move_forward(7)
player.turn_left()
player.collect("water")
player.turn_right()
player.move_forward(3)
player.turn_right()
sow_field("orange", 3, "right")
player.move_forward(3)
player.turn_right()
player.move_forward(3)
player.turn_right()
sow_field("pear", 4, "left")
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(3)
sow_field("apple", 3, "right")
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward(3)
sow_field("peach", 2, "left")