Advanced Python Development Course
Chapter
>
Level
Advanced Classes
Comparison Overloading
Objective
Check and label shipments correctly by using Comparison Overloading.
The shipping dock has some wine barrels that are ready to be taken out to stores, however then need to be labeled and prepped so that workers can pick them out and ship them without issue. To do this you must use Comparison Overloading, similar to Operator Overloading learned in the previous chapter, you must overload a class, this time with comparison operators.
There are three (3) orders in the dock, each with two (2) barrels. Each order has a sign meant to outline the weight ratio of the barrels in the order. There is a class outlined in the editor named barrel that allows you to use Comparison Overloading. There are also three (3) functions set up in the editor: less_than() , greater_than() , equal_to() , these functions are used to document the weight ratio on the signs.
Start off by walking to the dark and light X marks on the red carpets and face the barrels, use the read() function to determine the name of the wine in the barrel as well as the weight of the barrel, like this: await player.read(). Create objects for the dark and light barrels, using the barrel class, and add the information you identify from the read() function. Once the objects are set create another object that compares both objects. As an example:
red_barrel_dark = barrel("Wine name", 000) red_barrel_light = barrel("Wine name", 000) red_barrel_compare = red_barrel_dark < red_barrel_light
Be sure to write the name of the wine in the barrel class object exactly as it's displayed in the in the message when you use read().
Create the objects using the naming conventions according to the carpet and color X mark. For the red carpet, when comparing the barrels use the < operator to check if the dark barrel is lesser weight than the light barrel. To finish off this segment, walk to the gold X mark over the red carpet and face the sign. Use the function less_than() with the comparison object to chart the weight ratio of the barrels, like this: less_than(red_barrel_compare) .
Next walk to the dark and light X marks over the green carpet and repeat the same process. Use the read() function on dark and light X marks and create corresponding objects with that information. For the green carpets comparison object, this time around we will use the > operator to check if the dark barrel is a greater weight than the light barrel. Walk to the gold X mark over the green carpet and face the sign. Use the function greater_than() with the comparison object to chart the weight ratio of the barrels, like this: greater_than(green_barrel_compare) .
Walk to the dark and light X marks over the blue carpet and repeat the same process. Use the read() function on dark and light X marks and create corresponding objects with that information. For the blue carpets comparison object, this time around we will use the == operator to check if the dark barrel is an equal weight to the light barrel. Walk to the gold X mark over the blue carpet and face the sign. Use the function equal_to() with the comparison object to chart the weight ratio of the barrels, like this: equal_to(blue_barrel_compare) .
Once all orders have been labeled walk gold X mark over the orange carpet on the dock exit. Face the sign and use the write() function to chart down what’s on each order, write the names of each of the barrels according to the objects you created, for example when it comes to the red carpet barrels: red_barrel_dark.name , red_barrel_light.name . Do this for all six barrels in order to complete the level.