Hi! My name is HotRider. We will find hidden treasure on the map by playing the game Hot and Cold. Treasure is hidden on some of the tiles. My sensor values show how “hot” the surrounding tiles are. Help me reach the treasure by moving in the right direction.
In this lesson, we will learn to use conditional expressions in robotics coding. We need to use conditional statements to let the robot decide what action it will take based on whether a statement is true or false. For example, if it is true that the tile in front of us is ‘hotter’, then we may decide to move forward. If it is true that the tile to the right is ‘hotter’, then we we can turn right and move forward. A ‘hotter’ tile means the tile has a number bigger than the number we are currently on.
Checkpoint #1 – Your first conditional
The image below shows the situation you start in. If the tile in front is ‘hotter’ we need to move forward.
Try adding the following conditional to do this:
if robot.front == robot.center + 1:
Checkpoint #2 – Adding a conditional for the right turn
Checkpoint #3 – Adding the left turn and finding all the treasure
Checkpoint #4 – Using ‘elif‘ to reach the target with less energy.
Intead of using if commands, we can use elif (else if) so that only one conditional can execute on each loop. Look at the example code below. Both step1() and step2() will happen if condition2 is true after step1().
Now consider the same code but with elif. In this case step2() will not be called if step1() was called.