[Tutor] if...

Magnus Lycka magnus@thinkware.se
Mon Nov 11 20:01:24 2002


It's IDLE's fault (or your fault depending on how we
see it ;)

At 16:31 2002-11-11 -0500, William Allison wrote:
>if x < y:
>  print x, "is less than", y
>elif x >  y:
>  print x, "is greater than", y
>else:
>  print x, "and", y, "are equal"
>
>I can't seem to get this to work at all.

The above is correct. If you get Indentation Error it
didn't look like that. See for instance:

 >>> if x < y:
...     print x, "is less than", y
... elif x > y:
...     print x, "is greater than", y
...
 >>> if x < y:
...     print x, "is less than", y
...  elif x > y:
...     print x, "is greater than", y
...
Traceback (IndentationError: unindent does not match any outer indentation 
level (line 3)

(This is in typed in PythonWin)

>If I line up the 'elif'  with the 'print' I get SyntaxError: invalid 
>syntax from idle.

Of course, you could use an if there, but not an
elif.

>  When I line up the 'elif' with the 'if' I get: IndentationError: unident 
> does not match any outer indentation level (line 3).

IDLE 0.8 is a bit ugly in that it doesn't show a
secondary prompt. (Never thought about that before.)
Use the normal interpreter to see how it works there.
I would say that this is a bug in IDLE.

What you actually *type* is:
if bla bla:
         print bla bla bla
     elif bla bla:

Which looks like:
 >>> if bla bla:
         print bla bla bla
     elif bla bla:

Since the primary line is pushed in 4 positions by the
prompt, and the other lines aren't. This causes the
indentation error.

Normally the secondary prompt in python is "... "
which makes things even out...

Why is the secondary prompt in IDLE empty these days?
Is that just an oversight? (I tested with both IDLE 0.8
and IDLE fork 0.8.9. They both behave like that.)


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se