5 Replies - 422 Views - Last Post: 17 August 2011 - 05:33 PM Rate Topic: -----

#1 amy345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-August 11

Else statement error

Posted 17 August 2011 - 01:08 PM

I keep getting the following error "Indentationerror: unindent does not match any outer indentation level" for an else statemtent in an inbedded if statement. I've made sure the indention is correct, but still can't seem to figure out what is wrong. My script is trying to add one line of text to the next line text in a file.

l=""
v=[]
a=[]

f=open('stripf','r') 
o=open('stripf.out','w') 

for line in f:
   if l.startswith('  plotvar'): # find plot variables
      v=l.strip("\n")
      if line.startswith('  data'):
	     o.write(l)
	  else:
             o.write(v+line)
    elif line.startswith('  data'): # find first set of data
       a=line.strip("\n")
    elif line.startswith('   '):
       o.write(a+line)
    l=line   
f.close()
o.close()


My file looks like

  Header
  Header
  plotvar    time   var1   var3
  var3   var4
  data   0   0   0
   0   0

This post has been edited by amy345: 17 August 2011 - 01:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Else statement error

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Else statement error

Posted 17 August 2011 - 01:10 PM

EDIT: Nevermind, your error is on line 14 of what you've posted here. o.write(v+line) is indented two levels when it should only be intended one level.

This post has been edited by Brewer: 17 August 2011 - 01:11 PM

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: Else statement error

Posted 17 August 2011 - 01:13 PM

All text or all tabs: never mix.

Your code reads:
      if line.startswith('  data'):
\t     o.write(l)
\t  else:
             o.write(v+line)



While that might line up on your screen, the compiler won't like it.
Was This Post Helpful? 2
  • +
  • -

#4 amy345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-August 11

Re: Else statement error

Posted 17 August 2011 - 01:34 PM

Thank you! Sorry this wasn't a more interesting question.

I've been using Notepad++ as my texted editor and it automatically put the tabs in. I'm turning on the tabs and spaces viewer so hopefully it won't happen again.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: Else statement error

Posted 17 August 2011 - 03:10 PM

It wasn't that uninteresting. They call it "whitespace" for a reason. You're certainly not the first or last person to experience this. How do you think I spotted it so quickly? ;)

My editor (scite) does the same thing. Yes, my tab visibility is now on. I only use tabs, though a Python purist might argue for only spaces, four at a time.
Was This Post Helpful? 0
  • +
  • -

#6 aonbyte  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 08-October 10

Re: Else statement error

Posted 17 August 2011 - 05:33 PM

To avoid confusion I find it best to set your text editor to make tabs automatically four spaces.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1