Python robot challenges for beginners
73 Python programming challenges, in the order the course teaches them. Each one opens in the simulator with a starting program, and it is marked the moment you press Run: the robot either does the job or you are told what is missing.
0. Python quick start
- Cross the line The green zone starts 40 cm ahead. Drive long enough, or fast enough, to get there, then stop inside it. Lesson 0.1
- Two legs Forward moves you up the mat, right moves you across it. Both take distance= in centimetres. Lesson 0.2
- Four corners One side and one corner, four times over. A quarter turn is 90 degrees. Lesson 0.3
- Stop at the wall Drive while distance() is more than the gap you want, with a short wait each time round, then stop. Lesson 0.4
- Draw an L leg(cm) drives that far and then turns right. Call it with 40, then with 30. Lesson 0.5
- How near is the nearest tof_grid() hands back 64 whole-centimetre numbers. The bottom rows see the mat, so take min() over the rows that look straight ahead (rows 2 and 3). Lesson 0.6
1. Driving
- Cross the line The green zone starts 40 cm ahead. Drive long enough, or fast enough, to get there and stop inside it. Lesson 1.1
- Twenty, then forty Drive 20 cm, print `after 20:` followed by position(), then drive 40 more and stop in the green band (60 cm from the start). Lesson 1.2
- Face east Turn so the robot faces 90 degrees (east, to the right), then print `facing:` and its heading. Do not drive anywhere. Lesson 1.3
- Drive a square Visit the four corners in order and finish where you started, facing the way you started. Lesson 1.3
- Reach the green square The wall is in the way. Drive round it, or use distance() to find it. The robot coasts a little after stop(), so leave a margin. Lesson 1.4
- Gentle stop Stop with the robot's centre within 2 cm of the small square 50 cm ahead. Timing a drive is never that accurate: measure it. Lesson 1.5
- Draw an L Start at the top left facing down. Drive down the long stroke into the corner zone, turn, and drive the short stroke into the goal. That draws an L. Lesson 1.6
2. Sensing
- Report from the corner Get to the point 30 cm right and 30 cm up from where you start (forward then right, or right then forward), and print your position and heading in the form `at (30.1,… Lesson 2.1
- Stop before the wall Drive until distance() says the wall is close, then stop. The green zone is the band just before the wall. Lesson 2.2
- Widest gap Without moving, look at tof_grid() and print the column (0 to 7) with the largest reading: that is the way through. Lesson 2.3
- Dodge the box Drive until distance() says the box is close, print `box ahead`, step sideways until the way is clear, go past, and into the green zone. Lesson 2.4
- Straight and true This robot leaks sideways badly. Drive about 64 cm and finish inside the small square, still facing 0. Check position() and heading() as you go and correct with small… Lesson 2.5
- The maze Up the left channel, right along the top, down the middle channel into the goal. Use distance() to find each wall and turn_right(30, angle=90) at the corners. Lesson 2.6
3. Control
- Face north Spin until heading() is within 3 degrees of 0, printing `error:` and the error each time round the loop. Do not drive anywhere. Lesson 3.1
- Hold the heading This robot curves to the right whenever it drives. Steer against the heading error while driving, and finish in the green zone still facing 0. Lesson 3.2
- Hold the heading, fast Same robot, but the zone is narrower and you have 6 seconds. The starter's gain is far too high: it wobbles and wastes time. Tune the gain and the speed. Lesson 3.3
- Park at twenty Stop with the front of the robot 20 to 24 cm from the wall: the green band. Make the speed depend on how far you still have to go, and remember the motors do nothing… Lesson 3.4
- On the rails This robot curves AND leaks sideways. Two controllers at once: rotation from the heading error, sideways from the x error. The zone is only 6 cm wide. Lesson 3.5
- Precision parking Get to the bay at (77, 77) without touching the box, and finish facing 90 (east). The robot curves and leaks: hold your lines with feedback, then turn and hold the… Lesson 3.6
4. Vision
- Find the marker Switch the camera to the tag detector, read the tag in front of you, and print `marker 7 at 45.0 cm` (id and distance from the list). Do not drive. Lesson 4.1
- Face the marker Marker 3 is off to the right of the picture. Turn until it sits in the middle (cx near 160), then print `centred`. Do not drive. Lesson 4.2
- Dock at the marker Drive to marker 1 and stop about 12 cm in front of it, facing it. Steer from the tag's cx, let the distance set the speed, and square up at the end: the card faces down… Lesson 4.3
- Red or blue Look for each colour in turn and use cx to say which side it is on: print `left: <colour>` and `right: <colour>`. Do not drive. Lesson 4.4
- Hide and seek Marker 5 is somewhere around you but not in view. Spin until the camera sees it, print `found 5`, then drive up and stop about 14 cm in front of it. Lesson 4.5
- The marker trail Visit markers 1, 2 and 3 in order: for each one, spin until you see it, then drive up to it and stop close. The tags are on cubes, so they can be read from any side. Lesson 4.6
5. Behaviours
- Wander Keep driving for the whole 20 seconds, at least 120 cm in total, without touching a box or the edge. Look, decide, act, wait, repeat. Lesson 5.1
- Patrol in states Go from A to B and back again, round the box: up, right, then left, down. One state per leg, and a transition when the leg is done. Lesson 5.2
- Blink and drive Drive to the green zone while the LED blinks green and off every half second. Two things at once means a tick loop and a tick counter, not two waits. Lesson 5.3
- Seek, but safely Head for the green zone at (82, 82), but avoiding things comes first: when the distance sensor sees something close, deal with that before seeking again. Lesson 5.4
- Escape the box Three walls around you; the way out is on one side. Find it with the distance sensor, drive out, and into the green zone, without touching a wall. Lesson 5.5
- Rescue Find marker 9 with the camera, print `found 9`, drive to it, then come back home, all without touching the box. Search, approach and return are three states. Lesson 5.6
6. Seeing more
- See the line Switch the camera to the line detector and print where the line is, like `line at cx 197, angle 0.0`. Do not drive. Lesson 6.1
- Follow the line Follow the line to the green zone at the far end, staying within 6 cm of it. Steer from cx: the line left of centre means turn left. Lesson 6.2
- Find the line again The line has a gap. When it disappears, keep going a little, then sweep left and right until it is back. Finish in the green zone. Lesson 6.3
- Spot the robot Another robot is ahead to the right, wearing tag 101 with its LED lit. Read its tag for the distance and bearing, then its LED colour with the blob detector, and print… Lesson 6.4
- Follow the leader Robot 101 drives a loop at a steady speed. Stay 12 to 35 cm behind it for at least 80% of the run without touching it: steer from its bearing, speed from its distance. Lesson 6.5
- Bump and back There is a box the depth sensor cannot see (it is too low). Drive for the goal; when bumped() says you hit it, print `bumped`, back off, go round, and carry on. Lesson 6.6
- Give way Robot 102 shuttles back and forth across your path. Watch where it is going, wait when it is about to cross in front of you, and reach the goal without touching it. Lesson 6.7
- The delivery Follow the line to marker 7, print `found 7`, then follow it back to home, giving way to robot 102 both times. Lesson 6.8
7. Hands and feet
- Servo sweep Move servo 0 to 0, 45, 90, 135 and 180 degrees in turn, printing `servo 0 at <angle>` each time, with a short wait between. Do not drive. Lesson 7.1
- Grab the ball Drive up to the ball until it is just in front of the gripper, close the jaws with gripper("close"), and print `holding: red`. The jaws reach about 3 cm past the front… Lesson 7.2
- Fetch the ball The red ball is off to the right. Find it with the camera, drive up to it, grip it, carry it back into the home zone and let go. Lesson 7.3
- Kick it The ball lies 8 cm away, straight towards the green zone, but you are facing 30 degrees right of it, so the kicker cannot reach it yet. Turn to face the zone, then… Lesson 7.4
- Stop in the box The box is 23 to 31 cm ahead of the ball, too close for a kick. Carry the ball there instead: close the jaws on it, drive forward, and open them where you want it to… Lesson 7.5
- Pass to your partner Your team mate, robot 101, is waiting up to the right. Grip the ball, carry it to about 53 cm from the tag (a kick goes 45 cm and the ball starts 8 cm ahead of you),… Lesson 7.6
- Clear the mat Three red balls are scattered on the mat. Bring every one of them into the home zone: find, fetch, drop, repeat. Lesson 7.7
8. Learning
- See as numbers Take the level rows of the depth grid (rows 2 and 3, 16 numbers) and print `depth: min <cm>, max <cm>`. Do not drive. Lesson 8.1
- Collect data Visit the four spots in order (open, wall-ahead, gap-left, gap-right), record the level rows at each with its label, and print `recorded <label>` each time. Positions… Lesson 8.2
- Nearest neighbour Drive to the four spots (0, 15), (0, 40), (-38, 40), (38, 40) in turn and print `spot <n>: <label>` using nearest neighbour against DATA. Lesson 8.3
- A tiny network Train the network on DATA, printing the loss every 50 epochs, then print the training accuracy. Do not drive. Lesson 8.4
- Learn to wander Let the robot learn by reward: bumping costs, ground covered pays. It may bump early on; after 60 seconds it must not bump at all and must keep getting about the mat,… Lesson 8.5
- Tune yourself This robot leaks badly sideways. Drive up and down the lane, and let the program try different sideways gains, keeping whichever holds the lane best. After 30 seconds… Lesson 8.6
- A model from text MODEL is a model saved as text. Load it with json.loads, classify the current view with it, and print `model says: <label>`. Do not drive. Lesson 8.7
- Situations Get past the wall to the green zone using the classifier to decide what to do: open means go, wall-ahead means slide sideways, a gap means go through it. Print `model… Lesson 8.8
9. Talking to each other
- Call out Send `hello`, drive forward 20 cm, then send your position as `at <x>,<y>` (whole centimetres, from the start). Lesson 9.1
- Listen The Beacon sends `tick <n>` every second. Keep asking messages() and print `heard <sender>: <text>` for each one. Do not drive. Lesson 9.2
- Follow instructions Send `next` and the Guide replies with one instruction: `forward <cm>`, `right <cm>` or `done`. Do each one, ask again, and stop at `done`. You end up in the green zone. Lesson 9.3
- Meet the scout Ask the Scout `where are you?` and it answers `at <x>,<y>` measured from your start. Go and stop about 13 cm in front of it (the green square), without touching it. Lesson 9.4
- One at a time The corridor fits one robot. The Porter says `corridor busy` as it goes in and `corridor clear` as it comes out. Wait for clear, send `entering`, drive through to the… Lesson 9.5
- Share what you see Find the red ball with the camera, work out where it is from its bearing and its width, and send `ball at <x>,<y>` (from your start). The Fetcher cannot see it, but it… Lesson 9.6
- The relay Drive into the handover zone, stop there, and only then send `go`. The Runner sets off when it hears it and says `finished` at the far end. Stay in the zone. Lesson 9.7
10. Competing
- Stop on the line Drive fast while the line is far away, creep when it is close, and remember the robot slides a little after stop(). Lesson 10.1
- Park square Drive in fast while it is far, creep at the end, and check heading() before you stop: a quarter of a turn out is a fail. Lesson 10.2
- Through the traffic Two robots cross your path. Watch them with the camera tags and wait or go round. Lesson 10.3
- Thread the gap Each trail has a gap at one end, and the gaps are at opposite ends. Cross the mat between the trails, not over one. Lesson 10.4
- Call it and go Send attack, then drive to the far base. The message has to be the single word, and it has to go before you arrive. Lesson 10.5
- Carry it home The line bends to the right, and the ball is at its end, 15 cm to the side of where you start: drive straight and you miss it. Follow the line all the way, steering… Lesson 10.6
Questions
Are these Python challenges free?
Yes. Every challenge runs in the browser with nothing to install.
How are the challenges checked?
The simulator runs your program and checks what the robot did and what it printed against the challenge's goals. If something is missing it says what, so you can fix it and run it again.
Are there solutions?
There is no answer to copy. Each challenge has a Stuck? button that tells you what to work out next, and the lesson it comes from teaches everything the challenge needs.
What order should I do them in?
Top to bottom. They follow the course, so each section only needs what the sections before it taught.