Exercise 3.1 on Unit 3

I did exercise 3.1

Καταγραφή

in a different way, something like this.

from robot_control_class import RobotControl

rc = RobotControl()

laser = rc.get_laser(360)
if laser == float(2.500025987625122):
   rc.stop_robot()
else:
   rc.move_straight()

but I don’t know if this solution is correct because the writer has a different solution.

from robot_control_class import RobotControl

robotcontrol = RobotControl()

a = robotcontrol.get_laser(360)

if a < 1:
    robotcontrol.stop_robot()

else:
    robotcontrol.move_straight()

print ("The laser value received was: ", a)

Thanks in advance!

Hello @vasileios.tzimas ,

In programming, there are always many different ways in which you can solve an exercise/problem. The course notebooks just present one of them. If your code works OK and it accomplishes its purpose, then don’t worry if it’s not the same as the one in the notebook.

However, in general terms, it’s not a good idea to check for an exact value from a laser reading:

if laser == float(2.500025987625122):

It’s very likely that you will not get this exact value from the laser and, therefore, the robot will never enter the if statement and stop. It’s usually better to check for intervals. Does this make sense?

Thank you for your response! Yes, it was clear!