0

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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])

2 Comments

Hi Gregg, just as a heads up for next time, it's good ethos to mark @Dennis Shtatnov as the correct answer as he did provide you it after all ;)
Sorry, I hadn't been back in ages. Amended accordingly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.