0
votes

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

2

2 Answers

1
votes

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.

0
votes

After Dennis pointed out that I wasn't using Python, I took another go. This time it worked. :)

    from microbit import *

    alphabetIndex = 0
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    while True:
        if  button_a.is_pressed():
            if (alphabetIndex == 0):
                alphabetIndex = 26
            alphabetIndex = alphabetIndex - 1

        if  button_b.is_pressed():
            if (alphabetIndex == 25):
                alphabetIndex = -1
            alphabetIndex = alphabetIndex + 1

        display.scroll(alphabet[alphabetIndex])