Your FLL Team Should Probably Try Python
September 26, 2024
Most First Lego League teams start learning to code using Word Blocks. These colorful, puzzle-like pieces snap together with a simple drag-and-drop interface, making them approachable for beginners. You can quickly get a robot moving, and even begin to explore concepts like sensors. And Lego has a bunch of built-in lessons to get you going.
However, once they prepare for competitions, many teams find that their programs quickly become complex. A First Lego League robot completes multiple missions, usually through at least a few runs that span two and a half minutes. The code required for even a modest score can get out of hand. It can be difficult to debug, understand, and collaborate on.
The good news is that students finally have access to a more powerful language built into their Lego software: Python. While switching to Python can seem intimidating or even a bit scary, I believe that for most teams, Python actually makes a lot more sense when it comes to mission programming. It may take a little time to get the hang of, but once kids start using Python, they’ll find it opens up new levels of control and flexibility - and for debugging in the real world, Python can ironically be much easier.
Why Python Makes Things Easier
One of the core principles that kids learn in First Lego League is the importance of iteration: make a change, test it, then try something different and see if it improves. When code is written in text, it may be easier to change and understand over time. Python facilitates better understanding of code and makes it easier to make changes.
At first, students might be afraid of making mistakes, but Python’s error messages provide helpful guidance, and with time, they’ll find debugging easier than in block-based coding.
1. Named Functions Makes Cleaner Code
The core advantage comes from how easy it is to split code into functions. Experienced software engineers know that clean code is split into small functions that do one specific thing, rather than jumbling unrelated actions together in one list. This makes it easier to understand and decompose them.
In block-based coding, you can create MyBlocks, which are similar to functions. However, they are somewhat limited - you cannot call a MyBlock recursively, and it takes several clicks just to set them up. Because they are kind of a pain, many teams avoid or underuse functions. Last year, for example, my team used separate MyBlocks for each table run, and even with four or five, the kids had a hard time keeping track of them.
In Python, it’s easier to write narrow, specific functions, each with clear names:
async def put_arm_down():
"Touch the robot's arm to the ground."
await motor.run_for_time(port.A, 1000, 100)
People who read this code know that this function lowers the robot’s arm, even without needing to know the specifics of exactly how it’s done. And, if they want to lower the arm another time, even on another mission, then they can call the put_arm_down
function instead of remembering which motor sequence to invoke. There is even a docstring that will appear when this is called elsewhere, and the name will autocomplete so you don’t need to remember the exact name each time.
2. Print Statements for Easier Debugging
This is one of the biggest advantages of Python. If something isn’t working, we can throw in a print statement and see exactly what’s happening under the hood. With block programming, you’re stuck using lights or sounds from the robot to get feedback, which leaves students guessing.
In Python, you can print to the console with every action. students can follow the age old practice of print-and-check to trace the program flow and figure out what’s not working.
3. Easier to Copy and Paste
With block coding, rearranging code can quickly become messy. Yes, you can duplicate blocks, but soon you’ve got copies floating everywhere, and it gets overwhelming. In Python, everything is text. Copying, pasting, and moving code is super simple, making it easier for students to experiment and iterate on their work.
4. Access to Online Help
Here’s another bonus—Python errors are easy to look up. If a student gets stuck with a block-based error, they have to try to describe the issue and may struggle to find online help. With Python, you can copy and paste your error directly into Google or even ask AI tools like ChatGPT for help. This gives students an extra lifeline when troubleshooting problems.
Let’s See It In Action
Let’s see how Python compares to block-based coding by walking through an actual mission. This is my school’s team from 2023 executing Mission 9 of the Masterpiece board:
The robot drives forward, lowers its arm to hook a ring, moves the ring, lifts the arm, and drives back home. Even though the mission looks straightforward, writing the code to make it happen was a major feat that took a lot of experimentation.
Here are the Word Blocks they used to write this mission:
This code is only ten lines.
- First, drive the robot forward to the right spot.
- Then, drop the arm so that it lands within the ring.
- Push the robot forward so it hooks and grabs the ring.
- Pull it back.
- Release!
- Back up to home.
On our team, only one student on the team truly understood how it all worked. The others were hesitant to change anything once it was “working.” And that’s a problem because iteration - making small changes and testing - is key in robotics. If students are scared to tweak their code, they won’t learn as much.
Converting to Python
Now, here’s the Mission 9 code from above, when written in Python for Spike Prime:
async def do_mission_9():
"Run out and pull the boat back. Launched and returns to left home."
await forward(750)
await put_arm_down()
await m9_pull_ring_back()
await put_arm_up()
await m9_back_to_home()
async def put_arm_down():
"Lower the arm to the floor."
await motor.run_for_time(port.A, 1000, 100)
async def m9_pull_ring_back():
"For Mission 9, pull the ring back to the designated circle."
await motor_pair.move_for_degrees(motor_pair.PAIR_1, -360, -33)
await motor_pair.move_for_degrees(motor_pair.PAIR_1, -375, 0)
async def put_arm_up():
"Raise the robot arm."
await motor.run_for_time(port.A, 1000, -300)
async def m9_back_to_home():
"Return to home after end of mission 9."
await motor_pair.move_for_degrees(motor_pair.PAIR_1, -360, 33)
await motor_pair.move_for_degrees(port.C, -1000, 0)
Sure, it’s a little longer—16 lines instead of 10—but the big advantage here is readability and reusability. Students can now see at a glance what each function is doing - and some of the functions (like those that move the arm up and down) can be reused across other missions.
But isn’t it harder to learn?
A few years ago, using anything other than the Scratch-based language seemed daunting. With the older EV3 devices, you need to sideload via a flashed microUSB card to use Python or RobotC, and learn an entirely parallel stack. This made text-based languages a big challenge.
But with the latest release of Spike Prime v3, Python has been fully integrated into the main app. Students can code in Python with Bluetooth connections, even from a Chromebook. Debugging, motor detection, and an in-built Knowledge Base make Python a first-class language for Spike Prime without as many problems as we found in EV3.
One of the biggest hurdles is that the code can have error messages. So learning to read and interpret an error message is one of the core skills to be taught. Once they get the hang of it, Python allows more tools to debug and understand code which leads to less long term confusion.
Finally, AI tools not only help solve problems but also explain the logic behind fixes, making it a valuable learning tool for students. I am teaching them to carefully copy/paste their error messages and code into ChatGPT, which then gives excellent answers and can guide self-learning for problems that usually would need a mentor.
Ok, I’m ready! How do I start?
When you’re ready to get started, I recommend starting with the incredible Prime Lessons from the Seshan Brothers. There are also myriad Youtube channels - my favorite is the Creator Academy Australia). And, I’ve encouraged my students to read the Getting Started material that’s built in to the Spike Prime app.
If your team is ready to level up their coding skills, I encourage you to give Python a try. You’ll be amazed at how quickly they adapt and how much smoother competition missions become with text-based coding.