Teacher's Guide
Chapter 8 / Level 5: Operator Module
Teaching Objective:
- Student understands the benefits of using the operator module to simplify calculations
- Understands how to use operator functions to optimize code and reduce the amount of code needed to be written and objects used
- Learns the syntax and individual uses for the various operator module functions
Guide
In this exercise we will be working with the operator module, this allows you to shrink down operator sequences into easy to use functions.
import operator
For this exercise we will be using the following operator functions:
operators.eq(): Takes two arguments, check if the arguments are equal to each other.operators.lt(): Takes two arguments, checks if first argument is less than second argument.operators.gt(): Takes two arguments, checks if first argument is greater than second argument.operators.floordiv(): Takes two arguments, divides first argument by second argument and applies the equivalent ofmath.floor(), rounding the result down.operators.mod(): Takes two arguments, returns the remainder of the division between the two arguments.operators.contains(): Function is used with a list, takes two (2) arguments, the first argument is a list, the second is a value you would like to check if it’s inside the list. Returns:trueorfalsedepending on if the value is present or not.operators.concat(): Takes two list arguments, combines the two lists together using a single clean function, can be father than other methods.operators.countOf(): Takes two arguments, a list and a value to check how many times that value is present in the list. Returns the number of instances of the value is present.
Starting off we will be tackling the red , bule and green carpet X marks which will use the following corresponding variable constants: red_order , blue_order , green_order , which are orders for materials and: red_req , blue_req , green_req which are the required amount’s needed for the construction.
Walk to the red carpet X mark and read() the construction requirements, create a variable named a red_verification and use the operator.eq() with red_order and red_req as arguments. Use the red_verification variable with the pre-witten write() function.
await player.read()
red_verification = operator.eq(red_order,red_req)
player.write(
"Order is equal to request: %s" % (red_verification))
Do the same thing in the blue and green carpets, using read() and creating blue_verification and green_verification using the corresponding variable constants. The only difference being that on the blue carpet use operator.lt() and the green carpet use operator.gt() .
Walk to the light X mark, there are two constants in this segment: main_order and sub_order. Use read() for the orders that were already made, create a variable named allocations and use operator.floordiv() with main_order and the provided number you read in the order book, then charted down with the pre-witten write() function.
player.read()
allocations = operator.floordiv(main_order, -value read-)
player.write(
"Materials to allocate for each area: %s" % (allocations))
On the gold X mark, create a variable named remainder and use operator.mod() with main_order and sub_order then chart down the remainder variable using write().
remainder = operator.mod(main_order,sub_order)
player.write(
"Order remainder: %s" % (remainder))
This next segment uses two list constants: delivery_1 and delivery_2 . Walk to the X mark over the purple carpet and create a variable named tools , store the result of operator.contains() and add delivery_1 and "tools" as arguments, this will check if tools object is present on the list. Use write() to chart down the result.
tools = operator.contains(delivery_1,"tools")
player.write(
"Tools delivery status: %s" % (tools)
Walk to white carpet X mark and create a list named materials , store the value of operator.concat() withdelivery_1 and delivery_2 as arguments. Use the materials list with the pre-written write() function to chart down the result of combining both lists.
Finally use the speak() function with operator.countOf() and the materials list and the sting "wood" to check how many instances of "wood" are in the materials list in order to complete the level.
player.speak(operator.countOf(materials,"wood"))
Map overview and path

Code Solution
import player
import operator
player.move_forward()
player.turn_right()
await player.read()
red_verification = operator.eq(red_order,red_req)
player.write(
"Order is equal to request: %s" % (red_verification))
player.turn_left()
player.move_forward()
player.turn_right()
await player.read()
blue_verification = operator.lt(blue_order,blue_req)
player.write(
"Order is less than request: %s" % (blue_verification))
player.turn_left()
player.move_forward()
player.turn_right()
await player.read()
green_verification = operator.gt(green_order,green_req)
player.write(
"Order is greater than request: %s" % (green_verification))
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(2)
player.turn_left()
player.read()
allocations = operator.floordiv(main_order, 5)
player.write(
"Materials to allocate for each area: %s" % (allocations))
player.turn_right()
player.move_forward(3)
player.turn_left()
remainder = operator.mod(main_order,sub_order)
player.write(
"Order remainder: %s" % (remainder))
player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(3)
player.turn_right()
tools = operator.contains(delivery_1,"tools")
player.write(
"Tools delivery status: %s" % (tools))
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward(2)
player.turn_left()
materials = operator.concat(delivery_1,delivery_2)
player.write(
"All deliveries: %s" % (materials))
player.turn_right()
player.move_forward(2)
player.turn_left()
player.speak(operator.countOf(materials,"wood"))