Microbit python 语法无效

Microbit python Invalid syntax

我正在为我的孩子试用 BBC microbit 教育计算机。我想我会做一些简单的事情,比如遍历一个数组,使用按钮 A 和 B 左右递增(在末端循环)。我不知道我的代码有什么问题(在第 3 行报告语法错误)?另外,我关于 'input →' 和 'basic →' 与顶部微位导入相关的假设是否正确?

# 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

那是无效的 Python 代码。 Python 函数通常以 def main():

开头

前两行

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

虽然有效python。

后面的代码适用于 BBC Micro 的 'TouchDevelop' 环境。创建一个新的代码文件,如果您想尝试 运行 该代码,请确保 select TouchDevelop 编辑器。

丹尼斯指出我没有使用 Python 后,我又开始了。这次成功了。 :)

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