Python microbit basics: buttons and touch pins

Debug it with code
Find and fix the errors

At the time of writing in the UK, people are arguing and deciding whether to vote to stay in the European Union or vote to leave. The code below is broken (like the EU?) but is worth fixing (like the EU?).

Hold your own EU referendum using a micro:bit
Hold your own EU referendum using a micro:bit

When fixed it will let you have your own referendum by passing the micro:bit around the room for each person to press A or B. If you touch pin 0 for 3 seconds it will keep showing you the result of the vote, until you reset the micro:bit for another vote.

There are three deliberate errors:

A syntax error is stopping the code from running at all. You’ll see an error message telling you which line the error is on when you attempt to run the code. Compare that line with similar lines to see what’s missing.

runtime error doesn’t stop the code from running but does make it crash when it gets to a certain point in the code. Compare line 23 with 25, 27 and 29 and you’ll find a variable spelt incorrectly that causes a runtime error.

A logical error is making your code behave incorrectly: it does exactly what it’s told to do but its been told the wrong thing. See what happens when you vote for B and try to work out what’s gone wrong.

  • See if you can find and fix all three errors to make your own micro:vote counter:


5 thoughts on “Python microbit basics: buttons and touch pins

  • December 13, 2016 at 8:50 pm

    Wow – love this simulator. Haven’t managed to get into coding the micro:bit with anything other than the block editor up til now – this is LOADS better!

    Here’s my first program, part of a Christmas display I’ve stuck on my classroom door here in Cambodia:

    Press ‘A’ to announce the scroll the words of the angel.

    Press ‘B’ to play ‘Away in a manger’ one note at a time (had to add the durations to every note, doesn’t seem to ‘hold’ the previous value whilst playing single notes otherwise – bug or feature?)

    Q. How do I press A+B in the simulator?

    • December 15, 2016 at 1:47 pm

      I love your Christmas door display – thanks for sharing.

      I’ve added a button to let you press both A and B at the same time to the simulator – thanks for the suggestion.

      I don’t know about the bug / feature for playing notes – I’m in the process of updating the simulator to attempt to keep up with the new features of the micropython code (e.g. speech) so I’ll look into it.

      Merry Christmas 🙂

  • December 15, 2016 at 4:40 pm

    Speech would be great. I did try the speech library but it said not found. I then tried copying it across to the ‘official’ microbit editor – the code compiled but then the micro:bit said it was out of memory 🙁


Leave a Reply

Your email address will not be published. Required fields are marked *