文件写入错误

时间:2014-03-08 05:52:38

标签: 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"""
    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()#

我正在使用相扑模拟器进行微观交通模拟

1 个答案:

答案 0 :(得分:0)

Python将制表符视为8个空格,但许多文本编辑器将它们视为4. 从不混合制表符和空格。标准是使用四个空格,而不是标签。

除了这两行外,你在每一行都使用了空格:

with open("harsh1.txt","w") as harsh1:
    print >> harsh1, """hello"""

这些行缩进了标签。用空格替换它们。