gpt4 book ai didi

python - BBC Micro Bit 卡在代码上

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:11 24 4
gpt4 key购买 nike

我正在尝试在 BBC micro bit 上读取一个包含 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)

最佳答案

看起来您需要缩进从第一个 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

关于python - BBC Micro Bit 卡在代码上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43188863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com