Teacher's Guide

Chapter 5 / Level 6: Comparison Overloading

Teaching Objective:

  • Student understands what’s the different between Comparison Overloading and Operator Overloading
  • Understands how to create more complex operations with Python class objects and their benefits
  • Learns various use cases for Comparison Overloading and gets an idea of the full scope of Python Class programming

Guide

In this exercise we’ll be working with Comparison Overloading, this is similar to Operator Overloading but instead of using arithmetic operators between class objects you use comparison operators instead such as: < , > , = .

In this level you must check and chart down wine barrel orders for shipments. There is a class outlined in the editor named barrel as well as three(3) custom functions that allow you to display results of a comparison: less_than() , greater_than() , equal_to() .

Start off by walking to dark and light X marks over red carpets, face the barrels and use the read() function to find out the name and weight of the wine inside the barrel.

await player.read()

........

await player.read()

Create objects using the barrel class based on the information you obtained using the read() function. Create a variable that stores the results of their comparison using the < operator.

red_barrel_dark = barrel("Red Wine", 500)
red_barrel_light = barrel("Rose Wine", 462)
red_barrel_compare = red_barrel_dark < red_barrel_light

Format the objects and variable names according to the color of the carpets and color of the X marks. Walk up to the gold X mark over the red carpet, face the sign and use the less_than() function with the comparison variable.

less_than(red_barrel_compare)

Next walk to the green carpets and perform the same actions on the dark and light X marks, create the objects and variables in the same manner as in the red carpets except for the comparison use the > operator. In the same fashion as the previous set, walk to the gold X mark and for the green carpets use the greater_than() function.

greater_than(green_barrel_compare)

Next walk to the blue carpet X marks and repeat the process. Use the read() function on the dark and light X marks. Create objects and variables using the same conventions as previously done but for the comparison use the == operator. On the gold X mark over the blue carpet, and use the equal_to() function with the comparison variable.

equal_to(blue_barrel_compare)

Once all the orders have been tallied, walk to the gold X mark over the orange carpet and face the sign. Write the names of all the different wines on each barrel as directed in the editor.

await player.write(
"Order 1 contains %s and %s\nOrder 2 contains %s and %s\nOrder 3 contains %s and %s"
% (red_barrel_dark.name , red_barrel_light.name,
green_barrel_dark.name , green_barrel_light.name,
blue_barrel_dark.name , blue_barrel_light.name) )

Map overview and path

Map overview and path

Code Solution

import player

class barrel:
    def __init__(self, name, weight):
       self.name = name
       self.weight = weight

    # overload < operator
    def __lt__(self, other):
        return self.weight < other.weight
        
    # overload > operator
    def __gt__(self, other):
        return self.weight > other.weight
        
    # overload == operator
    def __eq__(self, other):
        return self.weight == other.weight

def less_than(comparison):
  if comparison:
    player.speak("The dark barrel is lighter than the light barrel.")
  else:
    player.speak("The light barrel is lighter than the dark barrel.")

    
def greater_than(comparison):
  if comparison:
    player.speak("The dark barrel is heavier than the light barrel.")
  else:
    player.speak("The light barrel is heavier than the dark barrel.")

    
def equal_to(comparison):
  if comparison:
    player.speak("The dark barrel is equal to the light barrel.")
  else:
    player.speak("The light barrel is not equal to the dark barrel.")

player.turn_left()
player.move_forward(3)
player.turn_left()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
await player.read()

red_barrel_dark = barrel("Red Wine", 500)
red_barrel_light = barrel("Rose Wine", 462)
red_barrel_compare = red_barrel_dark < red_barrel_light

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

less_than(red_barrel_compare)

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

green_barrel_dark = barrel("White Wine", 474)
green_barrel_light = barrel("Sparkling Wine", 443)
green_barrel_compare = green_barrel_dark > green_barrel_light

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

greater_than(green_barrel_compare)

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

player.turn_left()
player.move_forward()
player.turn_left()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
await player.read()

blue_barrel_dark = barrel("Rose Wine", 500)
blue_barrel_light = barrel("White Wine", 500)
blue_barrel_compare = blue_barrel_dark == blue_barrel_light

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

equal_to(blue_barrel_compare)

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

await player.write(
"Order 1 contains %s and %s\nOrder 2 contains %s and %s\nOrder 3 contains %s and %s" 
% (red_barrel_dark.name , red_barrel_light.name,
green_barrel_dark.name , green_barrel_light.name,
blue_barrel_dark.name , blue_barrel_light.name) )
Chapter 5 / Level 6: Comparison Overloading | Teacher's Guide