[Tutor] Problem with "elif"

Christian Mascher mascher@crg.ha.nw.schule.de
Sun, 25 Nov 2001 23:04:32 +0100


Pjotr <pjotr@poland.com> wrote:
> I started learning Python, using Python 2.1.1 and Ivan Van Laningham's
> book "Teach yoursefl Python...".  I have a problem with instruction
> "elif":
> >>> y = 1900
> >>> leap = "no"
> >>> if y % 400 == 0 :
>         leap = "yes"
>     elif y % 100 == 0 :
> 
> IndentationError: unindent does not match any outer indentation level (line 3)
> >>>
> 
> What's up? This example is from Laningham's book and everything should
> be OK. Please help me!

This is a feature from IDLE which has bothered me as well already, but
easy to explain I hope:
The if-statement starts with no indentation. Because you type it in
IDLE,
you have the shell-prompt '>>> ' on the first line. Although these are
four characters, they don't count for the indentation. So you shouldn't
align the elif with the if that you see on the screen, but with an
imaginary if at no indentaion level. Because all following lines have no
prompt (in IDLE-shell) they seem to start 4 characters earlier. In a
script-window you don't get this problem and also not in the simple
shell (command-line) where a '... '-prompt is inserted in every line
following the line with if.

paste from IDLE

>>> if 1==1:
	print 'bla'
elif 1==1:
	print 'blub'
else:
	print 'aha'

	
bla
>>> 

paste from shell:
>>> if 1:
...     print 'bla'
... elif 1:
...     print 'blub'
...
bla
>>>



HTH
Christian