Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Advanced Lists
Creating a Nested List

Objective

Store the seasonings properly in the spice rack by using nested lists.

One of the spice racks is bare and need to be properly restocked, there are seasonings in storage but they need to be fitted properly and properly organized and placed in the spice rack.

Because of the large amount of items and the fact they are stored in different shelves in a spice rack, normal lists and dictionaries aren’t going to be enough. For situations like these you can use Nested Lists, these are lists that contain one or more lists inside them.

In order to store spices you first need to grab jars in order to properly store the items, walk to the gold X mark, face the crate and use the collect() function to grab "empty jar" in order to facilitate the storage.

Once the jars are collected walk to the light X marks in front of wooden crates. There are four (4) list constants present in this level, one for each crate, these are: crate1,  crate2,  crate3, and crate4. Use the speak() function using these list constants to determine the content of each crate, the constants correspond the the crates from right to left, for example player.speak(crate1).

Once you determine the contents of the crates, use List Comprehensions to create new lists without any unnecessary items found inside the list constants. If the contents of a crate include: "package" , "string" or "stuffing" , use the same formula as in the previous chapter in order to curate the lists, for example:

crate1 = ["bay herb", "cilantro herb", "ginger herb", "oregano herb", "string"] herbs = [x for x in crate1 if not "string" in x]

The code above will create a new list with items that don’t include the word "string" in them. The lists should be named: herbs , spices , salts and sugars , you’ll know which list should be named as such because the items inside the lists carry the name.

Once all four lists have been created, it’s time to combine them all into a Nested List. Create a list named seasonings and append the lists you created inside the new list, like this:

seasonings = [ ] seasonings.append(herbs) .......

Once all items are put together in the Nested List , walk to the dark X mark on the green carpet and face the shelf. Use the speak() function in conjunction with the seasonings nested list in order to assert the items on each shelf. Afterwards use the place() function with the list in order to complete the level, like this: player.speak(seasonings).

Code book