Exercise 5.1 on Unit 5

Dear community,

In this exercise:


I have the following queries.
First, why are the two def used?

def move_straight(self):
        self.robotcontrol.move_straight_time(self.motion, self.speed, self.time)

    def turn(self):
        self.robotcontrol.turn(self.clockwise, self.speed, self.time_turn)

Secondly, at what point are they called?

Thanks in advance!

Hi @vasileios.tzimas,

  • For the first question, each def defines a method, also called function when defined outside of a class.
    We can define as many methods as we want.
    It is good advice to create small methods that do only one thing. This way, code is cleaner and easier to maintain.

  • For the second question, at the end of the picture in the question we have:

mr1 = MoveRobot('forward', 'clockwise', 0.3, 4)
mr1.do_square()

What “mr1.do_square()” does is call the method called do_square. By looking at the definition of the do_square method, we can see that at this point the methods move_straight and turn are called.

Please let us know if you still have questions.

Hi, @ralves again!

For the first question, I was not completely covered. I probably did not understand my question to you.
I want to understand what these two def are used for in our program. That is, what is def purpose.

For the second part, I do not see anywhere called, except do_square.

Hi @vasileios.tzimas,

it is good practice to give the functions the names of the actions that they perform.

In this case, the move_straight function does exactly this: move the robot straight ahead

The turn function does exactly what its name says: turn the robot

The same happens with do_square: the robot moves straight and turns 4 times, completing a square.

Please let me know if I misunderstand you.