0

I am getting an error of unexpected indent at (w) of with open

    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()#

I am using sumo simulator for microscopic traffic simulation

  • 3
    Make sure you're using *only* spaces (four spaces per indent), not mixing spaces with tabs. This code does not appear to have any necessarily bad indentation, but that could be due to changes you made when formatting it to post here. It's possible that everything from `with open...` onwards needs to be indented one step to fit inside the function though...? – Henry Keiter Mar 08 '14 at 05:57
  • Thanks very much, I used a tab istead of 4 spaces. The result was same but with spaces it started workin –  Mar 08 '14 at 05:59
  • @HenryKeiter, pro tip, if you click the [edit] button under the question, you can investigate for tabs vs spaces. Then just press cancel to avoid actually editing anything – mhlester Mar 08 '14 at 06:13
  • @mhlester Cool; I'd never noticed that! – Henry Keiter Mar 08 '14 at 17:11

1 Answers1

0

Python treats tab characters as 8 spaces, but many text editors treat them as 4. Never mix tabs and spaces. The standard is to use four spaces, not tabs.

You've used spaces on every line except these two:

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

Those lines are indented with tabs. Replace them with spaces.

mhlester
  • 22,781
  • 10
  • 52
  • 75