Teacher's Guide

Chapter 7 / Level 3: Else clause

Teaching Objective:

  • Student learns how to develop more intricate exceptions and how to format them
  • Learns the proper syntax of the Else clause and how to best make use of it in practical applications
  • Understands the versatility of the clause and practices examples of it’s implementation

Guide

In this exercise we’re going to be working with the else code block in exception handling. When paired with try / except , it will allows us to do more sophisticated checks as well as enables us to filter errors from a specific source.

For this level we will be consolidating construction materials and disposing waste materials. To start off walk to the light X mark in the left storeroom which you are directly in front of. There you will use collect() to grab a list constant named shingles, a list contains roof tiles used in repairs. Create a list named junk and a variable named red_storage, these are for disposing waste and consolidating items respectively.

player.collect(shingles)

red_storage = 0

junk = []

Now you use a for loop to take the items in the shingles list and determine then add them together to the red_storage variable while converting all items into whole numbers by using int().

for x in range(6):
  player.speak(int(shingles[x]))
  red_storage+=int(shingles[x])

This will cause an error because there are waste materials in the shingles list, use the try / except and else code blocks to filter all the waste materials from the list and only add the usable materials from the shingles list into the red_storage variable.

for x in range(6):
  try:
    player.speak(int(shingles[x]))
  except:
    player.speak("Item %s is junk" % (x))
    junk.append(shingles[x])
  else:
    red_storage+=int(shingles[x])

Once done, walk to the gold X mark in the left storeroom where you’re located and write() the tally for the singles you consolidated in the red_storage variable, then move on to the dark X mark over the red carpet and use place() to sore the red_storage variable in the crate.

player.write("There are %s shingles ready for use in storage" % (red_storage))

..............

player.place(red_storage)

After completing these tasks walk to the right side storeroom and make your way to the dark X mark over the green carpet. Once there, collect() the list constant named planks_set which holds sorted, useful planks for roof repairs. Make your way to the light X mark and use collect() again to acquire the shipment list constant from the crate, which is a recently delivered set with packaging materials. For this room you should also create another variable named blue_storage so you can consolidate items.

player.collect(planks_set)

...............

player.collect(shipment)

blue_storage = 0

Now that all elements have been brought together, convert with int() and add the shipment items into the planks_set list, then add the items into the blue_storage variable.

for y in range(6):

  planks_set[y]+=int(shipment[y])
  player.speak(planks_set[y])
  blue_storage+=int(planks_set[y])

Like before, errors will prevent the code from running because the shipment list contains waste materials, in this case it contains strings which will yield an error when added to numbers. To fix these issues use try / except and else in order to filter waste materials from the shipment list and add then consolidate plank materials successfully.

for y in range(6):
  try:
    planks_set[y]+=int(shipment[y])
    player.speak(planks_set[y])
  except:
    player.speak("Item %s is junk. Slot only has %s items" % (y,planks_set[y]))
    junk.append(shipment[y])
  else:
    blue_storage+=int(planks_set[y])

Once all useful plank materials have been added together in the blue_storage variable, walk to gold X mark and write() the tally of the amount of planks that are ready for use in blue_storage. Follow this up by walking to the dark X mark over the blue carpet and place() the blue_storage variable in the crate.

player.write("There are %s planks ready for use in storage" % (blue_storage))

...............

player.place(blue_storage)

Now that all repair materials have been consolidated, tallied and stored, walk to the dark X mark over the purple carpet and place() the junk list inside the trash chute in order to complete the level.

player.place(junk)

Map overview and path

Map overview and path

Code Solution

import player

player.move_forward(4)
player.collect(shingles)

red_storage = 0

junk = []

for x in range(6):
  try:
    player.speak(int(shingles[x]))
  except:
    player.speak("Item %s is junk" % (x))
    junk.append(shingles[x])
  else:
    red_storage+=int(shingles[x])

player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward()

await player.write(
  "There are %s shingles ready for use in storage" % ( red_storage))

player.turn_right()
player.move_forward(2)
player.turn_left()

player.place(red_storage)

player.turn_right()
player.move_forward(7)
player.turn_right()
player.move_forward()

player.collect(planks_set)
player.turn_left()
player.move_forward()
player.turn_right()
player.collect(shipment)

blue_storage = 0

for y in range(6):
  try:
    planks_set[y]+=int(shipment[y])
    player.speak(planks_set[y])
  except:
    player.speak("Item %s is junk. Slot only has %s items" % (y,planks_set[y]))
    junk.append(shipment[y])
  else:
    blue_storage+=int(planks_set[y])
    
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward()

await player.write(
  "There are %s planks ready for use in storage" % ( blue_storage ))
  
player.turn_left()
player.move_forward()
player.turn_right()

player.place(blue_storage)

player.turn_left()
player.move_forward(4)
player.turn_right()
player.move_forward(2)

player.place(junk)
Chapter 7 / Level 3: Else clause | Teacher's Guide