What can be the error in the code

I am stuck in Code Foundation for ROS - Exam - Python task 3.

The code i write works properly, the file name is also correct (task3.py), the code works perfectly fine when i test as it has class named ExamControl and functions get_laser_readings and main.

get_laser_readings first returns the values of the laser readings of left side and then it returns the value of the right side.

main starts robot move forward, towards the opening in the room, while moving forward, it keeps checking the values of the laser readings at the right and left sides of the robot and when these laser values indicate that there are no obstacles detected at the left or at the right side, the robot will stop its movement.

But when i submit the code i get 0 in task 3a and 3b

the code is attached below

class ExamControl:
        def __init__(self, motion, clockwise, speed, time):
            self.robotcontrol = RobotControl()
            self.motion = motion
            self.clockwise = clockwise
            self.speed = speed
            self.time = time
            self.time_turn = 7.0 # This is an estimate time in which the robot will rotate 90 degrees
        def get_laser_readings(self):
            a_right = self.robotcontrol.get_laser(0)
            a_left = self.robotcontrol.get_laser(719)
            print (a_left)
            print (a_right)
            return a_left, a_right
        def main(self):
            a_right = self.robotcontrol.get_laser(0)
            a_left = self.robotcontrol.get_laser(719)
            while (a_left != float('inf') or a_right != float('inf')):
                a_right = self.robotcontrol.get_laser(0)
                a_left = self.robotcontrol.get_laser(719)
                print (a_left)
                print (a_right)
                self.robotcontrol.move_straight_time()
                if a_left == float('inf') or a_right == float('inf'):
                    self.robotcontrol.stop_robot()
                    self.robotcontrol.shutdownhook()

Hi,
Can you insert your code with the “Preformatted text” mode? Without indentation a python code is meaningless.
Peter

@peterborkuti

class ExamControl:
        def __init__(self, motion, clockwise, speed, time):
            self.robotcontrol = RobotControl()
            self.motion = motion
            self.clockwise = clockwise
            self.speed = speed
            self.time = time
            self.time_turn = 7.0 # This is an estimate time in which the robot will rotate 90 degrees
        def get_laser_readings(self):
            a_right = self.robotcontrol.get_laser(0)
            a_left = self.robotcontrol.get_laser(719)
            print (a_left)
            print (a_right)
            return a_left, a_right
        def main(self):
            a_right = self.robotcontrol.get_laser(0)
            a_left = self.robotcontrol.get_laser(719)
            self.robotcontrol.move_straight_time(self.motion,self.speed,self.time)
            while (a_left != float('inf') or a_right != float('inf')):
                a_right = self.robotcontrol.get_laser(0)
                a_left = self.robotcontrol.get_laser(719)
                print (a_left)
                print (a_right)
                self.robotcontrol.move_straight_time(self.motion,self.speed,self.time)
                if a_left == float('inf') or a_right == float('inf'):
                    self.robotcontrol.stop_robot()
                    self.robotcontrol.shutdownhook()
                    

Hi @tabrar,

I see that the main method is inside the ExamControl. I don’t think you were asked to create a main method inside the ExamControl class.

The Prerequisites Exam fails because the system couldn’t find linux_exam. Please make sure your bash scripts are executable, if you have created any for the exam.

Please follow the instructions carefully, and also check the feedback provided by the grader bot.

I see that the highest and lowest laser values are not detected correctly. Are you correctly returning the values?

Bear in mind that the instructions do not ask you to print the values, but to return them. You can print them, of course, but in the end you have to return them.

Dear @ralves
In instructions for part 2 (python) there is no mention of linux_exam and any bash script. the instructions are as follows, but thanks i have also added the return command however the result is still the same for both of the tasks
[10:12:55][info] Checking Python Task 3…
[10:12:57][info] Python Task 3: task3.py found
[10:12:58][assess] Python Task 3: get_laser_readings method DID NOT work correctly
Check: - Adjust the Python script task3.py according to the instructions and specifications

[10:13:06][assess] Python Task 3: main method DID NOT work correctly
Check:- Adjust the Python script task3.py according to the instructions and specifications

Task 3

Inside the python_exam folder, create a new Python script, named task3.py. Inside this script, create a Python class named ExamControl.

The ExamControl class has to contain, at least, the following 2 methods:

  • get_laser_readings: This method, when called, returns the values of the laser readings of the right and left side of the robot (check Specifications).

  • main: This method, when called, makes the Turtlebot robot start the behavior described below:

  1. Initially, the robot starts moving forward, towards the opening in the room.
  2. While moving forward, your program keeps checking the values of the laser readings at the right and left sides of the robot.
  3. When these laser values indicate that there are no obstacles detected at the left or at the right side, the robot will stop its movement.

Check the Example section for more details on the expected behavior.

Specifications

  • The names of the class and the methods MUST BE exactly the same as the ones specified above.

  • Your final program (after you have tested that it works properly) MUST contain the ExamControl class, but MUST NOT contain any instance of the class.

  • The initial position of the robot for this exercise it’s the end position of the robot in the previous Exercise (check Fig. 1).

  • The get_laser_readings method has to return the values in an specific order: first, it returns the value of the left side and second, it returns the value of the right side.

  • We consider as laser readings from the left and right sides of the robot, the ones at the extremes of the laser values array. Remember the following image:

  • All the code of the class has to be contained INSIDE the class. This means, do not declare any variable or function outside the class.

Hi @tabrar,

linux_exam is indeed not mentioned in part 2 (Python), but I see you have already noticed that it is mentioned in Part 1 (Linux), since it is no longer failing for this reason.

I see you’ve got an 8 in the last submission, so, congratulations on that.

You are right about the main method inside the ExamControl class. I really missed that part when I first answered here.

Now, related to ExamControl, in the last code you pasted here, I see you added some parameters to the constructor (motion, clockwise, speed, time). Now, I’ve double-checked the instructions and I really didn’t see this being asked. If your class is expecting some parameters and the instructions don’t mention anything about them, if you try to instantiate that class just by calling c = ExamControl(), this is certainly going to fail.

Please, bear in mind that we use an automated system for the correction, so, the instructions must be followed strictly.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.