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?