2

I'm trying to read a file containing instructions for a turtle robot in micropython on the BBC micro bit but when I flash my code and add the correct files it just hangs and doesn't seem to do anything. It runs fine in python on my machine. Is micropython missing something I am doing but not telling me?

Here is the relevant code. A class is defined underneath this function but I haven't included that code as it's irrelevant to this bit.

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)
CapnMarcy
  • 31
  • 2
  • As currently shown in the question, it looks as if you have some indentation problems. Everything in a `with` block needs to be indented one level more than the `with`, and your second `with` has no indent so your function definition currently ends there. Once you've addressed that, have you tried adding some sort of diagnostics to the code to check how far it gets? Say, flashing a light or printing some debug output to the host - I'm not familiar with exactly what you can do on the microbit. – nekomatic Apr 04 '17 at 07:48
  • It doesn't appear to be running any of the code as if I set the first thing is does to displaying an LED it doesn't. – CapnMarcy Apr 07 '17 at 11:41

1 Answers1

1

It looks like you need to indent everything from the first for statement down to the return statement.

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
Gary Bake
  • 171
  • 7
  • Sorry for the late reply, I had messed up the formatting of the last bit only on stack overflow so that wasn't the issue sorry. – CapnMarcy Apr 07 '17 at 11:37