Teacher's Guide

Chapter 3 / Level 2: Creating a Nested List

Teaching Objective:

  • Student learns what a Nested list is and it’s uses
  • Understands how to put lists inside other lists after creating them
  • Has an example of how to perform complex operations using lists and further optimize code

Guide

In this exercise we’ll be working with Nested lists, these are lists that are inside other lists. for this level we’re going to be pulling out seasonings and putting them in jars so they can be stored in the spice rack.

Walk to the gold X mark in front of the crate and use collect() function to grab the "empty jar" .

player.collect("empty jar")

Walk to the light X marks in front of the crates and use the speak() function to identify the contents of the crates. There are four (4) list constants corresponding to the crates from right to left, these are: crate1,  crate2,  crate3, and crate4.

Use List Comprehensions to create new lists that remove unwanted items from the crates just like in the previous level. The new lists should be named: herbs , spices , salts and sugars in accordance with the items present inside the crates. The unwanted items to be removed from these new lists are: "package" , "string" or "stuffing" which you will identify if they are present after checking the contents in the crates.

player.speak(crate1)
herbs = [x for x in crate1 if not "string" in x]

player.speak(crate2)
spices = [x for x in crate2 if not "package" in x]

player.speak(crate3)
salts = [x for x in crate3 if not "stuffing" in x]

player.speak(crate4)
sugars = [x for x in crate4 if not "package" in x]

Once the new lists have been created, create a Nested List named seasonings and append() all the new lists you put together from the crates.

seasonings = [ ]

seasonings.append(herbs)

seasonings.append(spices)

seasonings.append(salts)

seasonings.append(sugars)

Finally, once you’ve created the Nested List, walk to to the dark X mark and use the speak() and place() functions to put the seasonings list on the shelf in order to complete the level.

player.speak(seasonings)

player.place(seasonings)

Map overview and path

Map overview and path

Code Solution

import player

player.move_forward(3)
player.turn_left()
player.collect("empty jar")
player.turn_right()
player.move_forward(2)
player.turn_left()
player.speak(crate1)
herbs = [x for x in crate1 if not "string" in x]
player.turn_left()
player.turn_left()
player.move_forward()
player.turn_left()
player.speak(crate2)
spices = [x for x in crate2 if not "package" in x]
player.turn_right()
player.move_forward(2)
player.turn_left()
player.speak(crate3)
salts = [x for x in crate3 if not "stuffing" in x]
player.turn_right()
player.move_forward(2)
player.turn_left()
player.speak(crate4)
sugars = [x for x in crate4 if not "package" in x]
player.turn_left()
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()

seasonings = [ ]

seasonings.append(herbs)
seasonings.append(spices)
seasonings.append(salts)
seasonings.append(sugars)

player.speak(seasonings)
player.place(seasonings)
Chapter 3 / Level 2: Creating a Nested List | Teacher's Guide