BBC Micro Bit Hanging on code
我正在尝试在 BBC 微型位上读取包含 micropython 中的海turtle机器人指令的文件,但是当我刷新我的代码并添加正确的文件时,它只是挂起并且似乎没有做任何事情。它在我机器上的 python 中运行良好。 micropython 是否遗漏了我正在做但没有告诉我的事情?
这里是相关代码。在此函数下定义了一个类,但我没有包含该代码,因为它与此位无关。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | def processInstFile(fileName): writeBuffer = [] writeBuffer.append("myturtle = turtle()\ ") with open(fileName,'r') as instFile: for line in instFile: line=line.strip() if line[0] =="#": pass else: if line.find("mf") != -1: writeBuffer.append("myTurtle.moveForward("+line[3:]+")\ ") if line.find("mb") != -1: writeBuffer.append("myTurtle.moveBackward("+line[3:]+")\ ") if line.find("t") != -1: writeBuffer.append("myTurtle.turnAngle("+line[2:]+")\ ") if line.find("pu") != -1: writeBuffer.append("myTurtle.penUp()\ ") if line.find("pd") != -1: writeBuffer.append("myTurtle.penDown()\ ") with open("turtleScript.py",'w') as instOutput: for line in writeBuffer: instOutput.write(line) return processInstFile("turtleinstr.py.p8l") display.show(Image.HEART) |
看起来您需要缩进从第一个 for 语句到 return 语句的所有内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | def processInstFile(fileName): writeBuffer = [] writeBuffer.append("myturtle = turtle()\ ") with open(fileName,'r') as instFile: for line in instFile: line=line.strip() if line[0] =="#": pass else: if line.find("mf") != -1: writeBuffer.append("myTurtle.moveForward("+line[3:]+")\ ") if line.find("mb") != -1: writeBuffer.append("myTurtle.moveBackward("+line[3:]+")\ ") if line.find("t") != -1: writeBuffer.append("myTurtle.turnAngle("+line[2:]+")\ ") if line.find("pu") != -1: writeBuffer.append("myTurtle.penUp()\ ") if line.find("pd") != -1: writeBuffer.append("myTurtle.penDown()\ ") with open("turtleScript.py",'w') as instOutput: for line in writeBuffer: instOutput.write(line) return |