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

Exception Handling
Else clause

Objective

Take repair materials and sort them, then discard unwanted waste by using else clause with exception handling.

Some repair materials have been stored at the top of the building, this is used to repair the roof after storms or general wear and tear. Some were recently delivered and are still stored in wrappers and containers, others have been stored for very long and were tossed in with junk. It would be a good idea sort the materials and properly store the materials and discard the junk and waste.

Because of the nature of the disorganized materials, you can likely expect errors. You can address this with the try / except code block however it can be a bit tricky if you’re unfamiliar with what errors you could run into. However you can expand your exception handling tools by adding the else clause. This allows you to run code if the except block isn’t run allowing to streamline your code and is well suited when identifying and cleaning up errors.

To start off you should walk into the left hand storeroom and walk towards the light X mark. This room stores shingles used to repair the roof tiles. Use the collect() function to pick up the list constant named shingles .

The list likely has several junk materials mixed in which need to be removed, more so they are stored in six (6) different compartments, each with their own amount of materials. You need to have all the materials consolidated into a single pile. Create a variable named red_storage to store all the materials and a list named junk so you can place all the waste materials.

Normally you would just identify the materials, convert them into whole numbers by int() and store them in a variable so you can put them away. But as you can probably expect, given the previous levels in the chapter, and the subject of this exercise, an error is bound to occur.

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

This will unfortunately crate a Value Error, as some of the items in the list appear to not be numbers, in order to remedy this you can use the try / except and else code blocks and sort the materials. By using the else block we effectively transform the except block into a filter that gets rid of unwanted elements in the list while the else blocks catches and transfers the good elements of the list into the variable we created.

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])

With this the red_storage variable now has all the usable shingles, where has all the trash has been added to the junk list. Walk to the gold X mark in the left storeroom you’re currently at, and use the write() function to chart down the number of shingles for storage, like this: await player.write("There are %s shingles ready for use in storage" % (red_storage)) Afterwards walk to the dark X mark over the red carpet and use the place() function to store the red_storage variable in the crate.

Once this task is done make your way to the right storeroom, this room stores wood planks and materials needed to repair roof and wall structures. Head for the dark X mark over the green carpet and use the collect() function to grab a list constant named planks_set from the crate, this list contains planks that have already been cleared of any waste. Walk to the light X mark next to it and use the collect() function again to grab another list constant named shipment, these are plank materials that were recently shipped and still contain some waste.

Much like in the previous room, the shipment list likely has several junk materials mixed in which need to be removed. Just like before they are stored in six (6) different compartments, each with their own amount of materials. Create a variable named blue_storage to store all the materials so you can consolidate them into a single pile. Like before, waste materials will be deposited in the junk list.

For the planks we are going to convert and add all the useful materials in the shipment list and add them to the planks_set list, then have them all consolidated, using int() ,in the blue_storage variable. However just like before, errors are to be expected.

for y in range(6): planks_set[y]+=int(shipment[y]) player.speak(planks_set[y]) blue_storage+=int(planks_set[y])

This time you can expect a Type Error, it’s what happens when you try to add numbers and strings together. This can be addressed by once again using the try / except and else code blocks, the try adds the lists together, the except block filters the junk from the list and the else clause adds the planks to the blue_storage .

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])

Now that the blue_storage variable is populated you can document and store all the wood planks. Walk to the gold X mark in the right storeroom you’re in, face the table and use the write() function to tally the materials you consolidated, like this: await player.write("There are %s planks ready for use in storage" % (blue_storage))

To finalize the level, walk to the dark X mark on the blue carpet and use the place() function to store the blue_storage variable in the crate. Then walk to the dark X mark over the purple carpet in the middle chamber and use the place() function to dispose of the junk list into trash chute, completing your task.

Code book