Teacher's Guide

Chapter 5 / Level 6: Assigning list values

Teaching Objective:

  • Student learns how to manually insert items in a list
  • Understands how to manage and transfer data between variables and lists
  • Practices more complex operations and forming data structures.

Guide

In this exercise the student must determine how many colored eggs were lain by the chickens in the barn, to do this you need to assign values into a list so you may tally up all the numbers.

First set up the lists so you may store the values for the colored eggs and get data on both the eggs and chickens.

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]

For ease of use, also set up shortcut variables so you may assign values to the various colored egg lists.

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

Once you set up the lists and data, walk to the dark X marks and use the speak() function to determine how many eggs of each color were incubated. Use constants: red_incubator , white_incubator and blue_incubator with the speak() function to check the values for each colored egg and insert them in the colored egg list.

player.speak(red_incubator)

red_eggs[kelly] = 2

.......

player.speak(white_incubator)

white_eggs[susan] = 1
white_eggs[betty] = 1
white_eggs[sandy] = 1

.......

player.speak(blue_incubator)

blue_eggs[susan] = 1
blue_eggs[betty] = 2

.......

Once all the values have been assigned, walk to the light X mark and use a for loop to read through all the data in the various lists in order to complete the level.

for x in range(4):
  player.speak("%s has lain %d %s, %d %s and %d %s"
  % (chickens[x], red_eggs[x], eggs[0],
  blue_eggs[x], eggs[1], white_eggs[x], eggs[2]))

Map overview and path

Map overview and path

Code Solution

import player

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]

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

player.turn_left()
player.move_forward()
player.speak(red_incubator)
red_eggs[kelly] = 2
player.turn_left()
player.move_forward(3)
player.turn_right()
player.speak(white_incubator)
white_eggs[susan] = 1
white_eggs[betty] = 1
white_eggs[sandy] = 1
player.turn_left()
player.move_forward(3)
player.turn_right()
player.speak(blue_incubator)
blue_eggs[susan] = 1
blue_eggs[betty] = 2
player.turn_left()
player.move_forward(2)
player.turn_left()

for x in range(4):
  player.speak("%s has lain %d %s, %d %s and %d %s" 
  % (chickens[x], red_eggs[x], eggs[0],
  blue_eggs[x], eggs[1], white_eggs[x], eggs[2]))
Chapter 5 / Level 6: Assigning list values | Teacher's Guide