Python Robot Maze
Write real Python — move(), loops and conditions — to steer a robot through six mazes.
Robot commands (API)
- move()
- Step one square forward. If there is no ground there the robot falls and the level restarts — so check first.
- turn_left()
- Rotate 90° anticlockwise. Does not move the robot.
- turn_right()
- Rotate 90° clockwise.
- can_move()
Trueif there is solid path directly in front.- can_move_left()
Trueif there is path to the robot's left.- can_move_right()
Trueif there is path to the robot's right.- at_goal()
Truewhen the robot is standing on the flag square.- on_gem()
Truewhen there is a gem on the current square.- collect()
- Pick up the gem under the robot.
- gems_left()
- How many gems are still uncollected.
- say(x)
- Write a message to the console — handy for debugging.
How the game works
The maze is a raised walkway floating in empty space — there are no walls to bump into, only edges to fall off. One wrong
move() and the robot drops into the void and the level restarts, so can_move() is not optional: it is how
you stay alive.
The robot understands eleven commands, listed under Robot commands above. Everything else is ordinary Python: you can
use variables, for and while loops, if/else, your own functions, even
print(). When you press Run, your program is executed on a real Python interpreter running inside your browser, the
robot's moves are recorded, and then played back on the board.
Level one needs four calls to move(). By level three, writing them out by hand becomes silly and a loop is the
obvious answer:
while not at_goal():
move()
Level five is where it stops being a toy. The maze has dead ends, and the classic solution is the right-hand rule: keep your right hand against the edge and you will eventually walk the whole maze. In Python that is three branches:
while not at_goal():
if can_move_right():
turn_right()
move()
elif can_move():
move()
else:
turn_left()
That is a real algorithm — the same one used to escape real mazes — expressed in eight lines of the language you are learning.
What you practise
- Calling functions and reading their return values (
can_move()gives you a boolean). - Loops: counted loops with
range(), and condition loops withwhile. - Conditions:
if,elif,elseandnot. - Debugging: when the robot stops in the wrong place,
say()shows you what it was thinking. - Decomposition: by level six most players write a helper function such as
def step_and_collect():.
Tips if you get stuck
- Read the robot, not the code. Watch where it actually stops — or falls — and that square tells you which branch ran.
- Check before you move.
if can_move(): move()is always safe; a baremove()over the edge is fatal. - Print the state.
say(can_move(), at_goal(), gems_left())inside the loop makes the logic visible. - Beat par second. Solve it any way you can first, then try to do it in fewer actions.
Frequently asked questions
How do I play Python Robot Maze?
Type Python in the editor and press Run. Your code controls the robot through functions like move(), turn_right() and can_move(). The maze is a narrow walkway floating in empty space: reach the flag without stepping off it, and on the last level collect every gem first.
What happens if my loop never ends?
The game stops your program automatically: after 2,000 robot actions it raises an error, and a loop that does nothing at all is terminated after a few seconds. You cannot lock up the page.
Is this really Python?
Yes. Your code runs on CPython compiled to WebAssembly, in a background worker. Syntax errors, indentation errors and exceptions all behave exactly as they would on a computer.
Which Python concepts does the maze teach?
Calling functions, for and while loops, if/elif/else, boolean logic and — by level five — thinking in algorithms, since the wall-following rule is a genuine maze-solving algorithm.
What happens if the robot steps off the path?
It falls into the void, the run stops and the level restarts from the beginning — your code stays in the editor so you can fix it. Guard every move with can_move() and it will never happen.