2

我正在尝试在 BBC micro 位上读取包含 micropython 中的海龟机器人指令的文件,但是当我刷新代码并添加正确的文件时,它只是挂起并且似乎没有做任何事情。它在我机器上的 python 中运行良好。micropython 是否缺少我正在做但没有告诉我的事情?

这是相关的代码。在此函数下定义了一个类,但我没有包含该代码,因为它与该位无关。

def processInstFile(fileName):
    writeBuffer = []
    writeBuffer.append("myturtle = turtle()\n")
    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:]+")\n")
            if line.find("mb") != -1:
                writeBuffer.append("myTurtle.moveBackward("+line[3:]+")\n")
            if line.find("t") != -1:
                writeBuffer.append("myTurtle.turnAngle("+line[2:]+")\n")
            if line.find("pu") != -1:
                writeBuffer.append("myTurtle.penUp()\n")
            if line.find("pd") != -1:
                writeBuffer.append("myTurtle.penDown()\n")
    with open("turtleScript.py",'w') as instOutput:
        for line in writeBuffer:
            instOutput.write(line)
    return

processInstFile("turtleinstr.py.p8l")
display.show(Image.HEART)
4

1 回答 1

1

看起来您需要缩进从第一个 for 语句到 return 语句的所有内容。

def processInstFile(fileName):
    writeBuffer = []
    writeBuffer.append("myturtle = turtle()\n")
    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:]+")\n")
                if line.find("mb") != -1:
                    writeBuffer.append("myTurtle.moveBackward("+line[3:]+")\n")
                if line.find("t") != -1:
                    writeBuffer.append("myTurtle.turnAngle("+line[2:]+")\n")
                if line.find("pu") != -1:
                    writeBuffer.append("myTurtle.penUp()\n")
                if line.find("pd") != -1:
                    writeBuffer.append("myTurtle.penDown()\n")
    with open("turtleScript.py",'w') as instOutput:
        for line in writeBuffer:
            instOutput.write(line)
    return
于 2017-04-04T07:51:27.407 回答