Python Development Course
Chapter
>
Level

Creating Lists
Assigning list values

Objective

Determine how many eggs of each color were lain and incubated by each chicken by assigning list values.

The chickens have lain several eggs which were placed in incubators, find out which chicken laid the eggs and how many of each color. The names of the chickens and the different types of colored eggs have been stored in two (2) different string lists which are as follows.

chickens = ["Susan", "Kelly", "Betty", "Sandy"] eggs = ["red eggs", "blue eggs", "white eggs"] red_eggs = [0, 0, 0, 0] blue_eggs = [0, 0, 0, 0] white_eggs = [0, 0, 0, 0]

Numeric lists are also created for each of the three (3) different egg colors, these being set up so we may fill them with amounts of each egg lain. The four entries represent each of the four chickens that laid eggs. These can be set as variables for ease of calling later.

susan = 0 kelly = 1 betty = 2 sandy = 3

Walk to the dark X marks next to the incubators and use the speak() function to determine which chicken laid eggs of that color. Each incubator has a constant with a message outlining the amounts, those being : red_incubator , blue_incubator and white_incubator . Read the message in each incubator of the correct colored egg, like this: player.speak(red_incubator) .

Once you read a message, add the number of eggs lain to the colored egg list. Do this by referencing the position in the list and assigning a value, here is an example:

# Message ( "Kelly laid 2 Red eggs" ) red_eggs[kelly] = 2 # This is the same as writing - red_eggs[1] = 2

Once you have successfully read all the messages and added the amounts to the colored eggs list, walk to the light X mark, face the table and use a for loop provided in the editor to read out all the names and amounts of eggs incubated in order to complete the exercise.

Code book