user1108473 user1108473 - 5 months ago 6
Python Question

Microbit python Invalid syntax

I'm trying out the BBC microbit educational computer for my kids. I thought I'd do something simple, like traverse an array, using buttons A & B to increment left and right (looping at the ends). I can't work out what's wrong with my code (reports syntax error on line 3)? Also is my presumption about 'input →' and 'basic →' relating to the microbit import at the top correct?

# Add your Python code here. E.g.
from microbit import *
function main ()
var alphabet := ""
var alphabetIndex := 0
input → on button pressed(A) do
if alphabetIndex = 1 then
alphabetIndex := 27
else add code here end if
alphabetIndex := alphabetIndex - 1
end
input → on button pressed(B) do
if alphabetIndex = 26 then
alphabetIndex := 0
else add code here end if
alphabetIndex := alphabetIndex + 1
end
basic → forever do
basic → show number(alphabetIndex, 150)
end
for 0 ≤ i < 1 do
alphabetIndex := 1
alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end for
basic → show string(alphabet[alphabetIndex], 150)
end function

Answer

That is not valid Python code. Python functions usually start with def main():

The first two lines with

# Add your Python code here. E.g.
from microbit import *`

are valid python though.

The code following that is intended for the 'TouchDevelop' environment for the BBC Micro. Make a new code file and be sure select the TouchDevelop editor if you would like to try and run that code.