Coding Python
Home›Python Coding Games›Python Robot Maze

Python Robot Maze

Write real Python — move(), loops and conditions — to steer a robot through six mazes.

Level 1Reach 🏁 — do not fall
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()
True if there is solid path directly in front.
can_move_left()
True if there is path to the robot's left.
can_move_right()
True if there is path to the robot's right.
at_goal()
True when the robot is standing on the flag square.
on_gem()
True when 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.
Your Python

1

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 with while.
  • Conditions: if, elif, else and not.
  • 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

  1. Read the robot, not the code. Watch where it actually stops — or falls — and that square tells you which branch ran.
  2. Check before you move. if can_move(): move() is always safe; a bare move() over the edge is fatal.
  3. Print the state. say(can_move(), at_goal(), gems_left()) inside the loop makes the logic visible.
  4. Beat par second. Solve it any way you can first, then try to do it in fewer actions.
Write longer programs on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
Get it free

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.

← All Python coding games  Try Bug Squash →