Editor

Which Way Now?

Beginner · Write the code · from Grid Robot Algorithms: MOVE_FORWARD, ROTATE and CAN_MOVE

From the lesson: Grid Robot Algorithms: MOVE_FORWARD, ROTATE and CAN_MOVE

Many exam questions show a robot on a grid and ask which code segment gets it to a goal square, using four commands from the reference sheet: MOVE_FORWARD(), ROTATE_LEFT(), ROTATE_RIGHT() and CAN_MOVE(direction). The small Python robot on this page runs any path you write, so you can check a trace instead of guessing.

Read the lesson

Write final_facing(start, turns). start is "up", "right", "down" or "left", and turns is a string of turns: "L" for ROTATE_LEFT() and "R" for ROTATE_RIGHT(). Return the way the robot faces after all the turns.

final_facing("up", "R")     ->  "right"
final_facing("up", "LL")    ->  "down"
DIRECTIONS = ["up", "right", "down", "left"] # clockwise order
def final_facing(start, turns):
# Walk along DIRECTIONS: +1 for a right turn, -1 for a left turn
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
final_facing("up", "R")'right'
final_facing("up", "LL")'down'
final_facing("left", "RRRR")'left'

More Grid Robot exercises

All Python exercises · 50 practice problems