Teacher's Guide

Chapter 8 / Level 1: Code Introspection

Teaching Objective:

  • Student understands the concept behind code introspection and it’s uses
  • Becomes familiar with the various functions associated to code introspection
  • Learns the nuances of how to display the information using various functions and how to get relevant information for debugging

Guide

In this exercise, we will be working with code introspection, particularly the following functions: type() , id() , repr() , dir() , help() . These functions are used to acquire data from a specific object and outline it’s uses, the exception being the repr() function which is used to convert values into strings, useful to process the data compiled by the other functions. We will also be using the new display() async function.

The objective of the level is to find out more information of old items stored in the attic chests and catalogue them. There are three (3) constants with unknown values named: red_container , green_container , and blue_container . Walk to the dark X marks over the colored carpets in front of the chests, open() the chests and collect() corresponding constant matching the colored carpet. Use type() with the constant to identify it’s type and use repr() to convert the data to string. Finally use speak() to announce the type and speak() again to verify the contents. Do this for all three (3) X marks and constants.

player.open()

player.collect(red_container)

player.speak(repr(type(red_container)))

player.speak(red_container)

# Do this for green and blue containers

Walk to the light X mark by the desk and face the memo, create three variables named: item_id_red , item_id_green , item_id_blue they will be used to store ids by using the id() function with the corresponding color coded constant. Store all three (3) variables on a list named item_list and use display() to ensure it’s noted on the desk.

item_id_red = id(red_container)

item_id_green = id(green_container)

item_id_blue = id(blue_container)

item_list = [ item_id_red, item_id_green , item_id_blue ]

await player.display(item_list)

Walk to the dark X marks over the purple carpet, use dir() to find out the item_list attributes. Use the repr() function to convert the data into a string and store it in a variable named item_attributes . Use display() with item_attributes ensure it’s noted on the desk.

With the dark X mark over the white carpet, use help() instead of dir() in a variable named item_help and use read() to verify the help() options.

item_attributes = repr(dir(item_list))

await player.display(item_attributes)

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

item_help = repr(help(item_list))

await player.read()

Map overview and path

Map overview and path

Code Solution

import player

player.turn_right()
player.move_forward(4)
player.turn_right()
player.open()
player.collect(red_container)
player.speak(repr(type(red_container)))
player.speak(red_container)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.open()
player.collect(green_container)
player.speak(repr(type(green_container)))
player.speak(green_container)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.open()
player.collect(blue_container)
player.speak(repr(type(blue_container)))
player.speak(blue_container)

player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_left()

item_id_red = id(red_container)

item_id_green = id(green_container)

item_id_blue = id(blue_container)

item_list = [ item_id_red, item_id_green , item_id_blue ]

await player.display(item_list)

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

item_attributes = repr(dir(item_list))
await player.display(item_attributes)

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

item_help = repr(help(item_list))
await player.read()
Chapter 8 / Level 1: Code Introspection | Teacher's Guide