我试图在BBC micro bit上读取一个包含关于乌龟机器人指令的文件,但当我刷新我的代码并添加正确的文件时,它只是挂起而且似乎什么都不做。它在我的机器上运行python很好。我是在做什么,而不是告诉我吗?
这是相关代码。在此函数下面定义了一个类,但我还没有包含该代码,因为它与此位无关。
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)
答案 0 :(得分: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