Https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-code-challenges-ii/articles/advanced-python-code-challenges-classes

Hello,
There is something I don’t understand in the behaviour of the code in this exercise:

class Robot:

    all_disabled = False

    latitude = -999999

    longitude = -999999

    robot_count = 0

    def __init__(self, speed = 0, direction = 180, sensor_range = 10):

        self.speed = speed

        self.direction = direction

        self.sensor_range = sensor_range

        self.obstacle_found = False

        Robot.robot_count += 1

        self.id = Robot.robot_count

    def control_bot(self, new_speed, new_direction):

        self.speed = new_speed

        self.direction = new_direction

    def adjust_sensor(self, new_sensor_range):

        self.sensor_range = new_sensor_range

    def avoid_obstacles(self):

        if self.obstacle_found:

            self.direction = (self.direction + 180) % 360

            self.obstacle_found = False

class DriveBot(Robot):

    def __init__(self, motor_speed = 0, direction = 180, sensor_range = 10):

        super().__init__(motor_speed, direction, sensor_range)

class WalkBot(Robot):

    def __init__(self, steps_per_minute = 0, direction = 180, sensor_range = 10, step_length = 5):

        super().__init__(steps_per_minute, direction, sensor_range)

        self.step_length = step_length

    def adjust_sensor(self, new_sensor_range):

       super().adjust_sensor(new_sensor_range)

       self.obstacle_found = False

       self.step_length = 5

   # Override the avoid_obstacles method here!

    def avoid_obstacles(self):

      if self.obstacle_found == True:

        if self.speed <= 60:

          super().avoid_obstacles()

          self.speed /= 2

          self.step_length /= 2

        else:

         self.direction = (self.direction + 90) % 360

         self.speed /= 2

         self.step_length /= 2   

robot_1 = WalkBot(150, 0, 10, 10)

robot_1.obstacle_found = True

robot_1.avoid_obstacles()

print(robot_1.direction)

print(robot_1.speed)

print(robot_1.step_length)

robot_2 = WalkBot(60, 0, 20, 40)

robot_2.obstacle_found = True

robot_2.avoid_obstacles()

print(robot_2.direction)

print(robot_2.speed)

print(robot_2.step_length)

We were required to override the avoid_obstacle method.
I eventually got it to work, but if I used self.steps_per_minute instead of self.speed in if self.speed <= 60 I kept getting the error “steps_per_minute not an attribute of WalkBot”; even though in an earlier exercise we were asked to instantiate WalkBot with steps_per_minute as speed ( The next class is the WalkBot class. This class should also use the Robot constructor, but the speed is represented by steps_per_minute which you can pass into the superclass constructor.), hence the parameter and the self.steps_per_minute in the init ( as far as I understand).
Can someone please shine some light on this issue and the mechanism please?

If I’m understanding you correctly you tried to use self.steps_per_minute but it threw errors? Although you pass a parameter with that name there’s no assignment to an attribute with this name so there is no .steps_per_minute attribute (which the error mentions).

Parameters you pass to __init__ aren’t automatically bound as attributes, that’s what your assignment statements do (there’s no requirement that the parameter names are then bound to a similar attribute name).

If you follow the flow when you pass these parameters you’ll find what attribute you acutally assign the value of steps_per_minute to.

2 Likes

Thanks for the reply!
I’ve followed the flow more carefully as you indicated and concluded I was making a confusion, I hope I have it right now:
If I understand correctly, I am assigning the generic steps_per_minute (which could have as easily been called steps_per_moon) to the attribute self.speed (through the super init), which is to say I create the variable self.speed = to the input in the spot of the parameter steps_per_minute.

2 Likes