Python 文件写入错误
我得到一个错误的意外缩进在w的开放Python 文件写入错误,python,traffic-simulation,Python,Traffic Simulation,我得到一个错误的意外缩进在w的开放 def run(): """execute the TraCI control loop""" traci.init(PORT)# programPointer = len(PROGRAM)-1 step = 0# with open("harsh1.txt","w") as harsh1: print >> harsh1, """hello"""
def run():
"""execute the TraCI control loop"""
traci.init(PORT)#
programPointer = len(PROGRAM)-1
step = 0#
with open("harsh1.txt","w") as harsh1:
print >> harsh1, """hello"""
while traci.simulation.getMinExpectedNumber() > 0:#
traci.simulationStep()#
programPointer = min(programPointer+1, len(PROGRAM)-1)
no = traci.inductionloop.getLastStepVehicleNumber("0")#"0" is the detector id
if no > 0:#
programPointer = (0 if programPointer == len(PROGRAM)-1 else 3)
traci.trafficlights.setRedYellowGreenState("0", PROGRAM[programPointer])#
step += 1#
traci.close()#
sys.stdout.flush()#
我正在使用sumo模拟器进行微观交通模拟Python将制表符视为8个空格,但许多文本编辑器将其视为4个空格。不要把标签和空格混在一起。标准是使用四个空格,而不是制表符 除了这两行之外,您在每一行上都使用了空格:
with open("harsh1.txt","w") as harsh1:
print >> harsh1, """hello"""
这些行用制表符缩进。将它们替换为空格。确保每个缩进仅使用空格四个空格,而不是将空格与制表符混合使用。这段代码似乎没有任何不必要的错误缩进,但这可能是由于您在将其格式化为此处发布时所做的更改。有可能所有的东西都是开放的。。。向前需要缩进一步以适应函数的内部,但是…?非常感谢,我使用了4个空格的选项卡istead。结果是一样的,但用空格开始workin@HenryKeiter,pro提示,如果单击问题下的按钮,可以调查选项卡与空格。然后按“取消”以避免实际编辑anything@mhlester凉的我从来没注意到!