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

Math Modules
Operator Module

Objective

Verify and allocate the new farmland construction material orders by using operator module.

The upper floor of the annex has some offices that hold logs of the inventory and orders for the construction materials needed for the new land development. There are a variety of different tools that could be used to verify and calculate the materials so they can be used for the construction, aside from the math and statistics module we can use the operator module.

This module , import operator , can be used for simplifying operator calculations and conveniently packing them into functions. You can use these functions for both optimization as well as for producing values without needing to create an object to store the values for the calculations.

  • 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 of math.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: true or false depending 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.

To start off, walk to the dark X marks over the red , blue and green colored carpets. There are six (6) 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.

At the dark X mark over the red carpet, use the read() function to check the required amount of materials. Create a variable named red_verification and use the operator.eq() function with the red_order and red_req constants, like this: red_verification = operator.eq(red_order,red_req) . With the pre-written write() function use the red_verification to display the result.

On the blue and green X marks repeat the same process, use the read() function and create the variables: blue_verification and green_verification . For the blue variable store the value of operator.lt() using the blue_order and blue_req constants to check if the order is less than what is required. For the green variable store the value of operator.gt() using the green_order and green_req constants to check if the order is greater than the materials required. Use the pre-written write() functions with the respective verification functions corresponding to the colored carpets.

Next walk to the light X mark and on the desk use the read() function to check orders that have already been placed and are in route. In the order book you will find the values of two variable constants: main_order and sub_order as well as how many sections the materials need to be divided in. After reading the order book, create a variable named allocations and store the value of operator.floordiv() to divide the main_order value by the amount of sections described in the book, like this: allocations = operator.floordiv(main_order, -add sections-) this divide the order and round down the result. Use the pre-written write() function with the allocations variable to chart down the result.

Walk to the gold X mark and create the a variable named remainder , store the result of operator.mod() and add both main_order and sub_order constants to produce the remainder of the two orders. On the desk, use the pre-written write() function and add the remainder variable to chart down the result.

Make your way to the dark X mark over the purple carpet, here we will be verifying if the delivery of the tools needed for the construction. We have two lists constants, delivery_1 and delivery_2, we will be checking if the tools were delivered in the first delivery. Create a variable named tools and store the result of operator.contains(), for the first argument add the delivery_1 list and for the second argument add the string “tools” . This will check if the tools are present in the delivery, like this: tools = operator.contains(delivery_1,"tools") . Once that’s done, on the desk, use the pre-written write() function with tools variable to chart down if the tools were successfully delivered.

Next up is checking all the deliveries, make your way to the dark X mark over the white carpet and create a list named materials . Use it to store the value of operator.concat(), using the delivery_1 and delivery_2 lists as arguments. What this does is that it quickly merges the two lists into a single list. On the desk, use the pre-written write() function with materials variable to make note of all of the materials that were already delivered for the construction.

Lastly walk to the dark X mark over the orange carpet, here there’s some prerequisites written for starting construction, one of the main notations is how much wood is needed at the start in tonnage. Check how much wood was delivered in the entire delivery and announce it, use the speak() function with the operator.countOf() function. For the first argument add in the materials list you created followed by the string “wood” , this will return how many instances of wood are present in the material list. This illustrates how operator functions can be used standalone without the use of an object to store it in, like this: player.speak(operator.countOf(materials,"wood")) . This will complete the level.

Code book