我正在为我的孩子们试用英国广播公司的微型教育电脑。我想我应该做一些简单的事情,比如遍历数组,使用A&B按钮来增加左和右(在末端循环)。我不知道我的代码有什么问题(在第3行报告语法错误)?另外,我关于“输入 →”和“基本 →”的假设是否与最上面的微位导入有关?
# 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
发布于 2016-07-01 23:21:56
这是无效的Python代码。Python函数通常从def main():开始
前两行
# Add your Python code here. E.g.
from microbit import *`都是有效的蟒蛇。
下面的代码是为英国广播公司的“TouchDevelop”环境准备的。创建一个新的代码文件,如果要尝试运行该代码,请确保选择TouchDevelop编辑器。
发布于 2016-07-01 23:54:14
丹尼斯指出我没有使用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])https://stackoverflow.com/questions/38154623
复制相似问题