classification
Title: indentation problem in if, elif, else statements
Type: behavior Stage:
Components: Regular Expressions Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: loewis, rhettinger, victorywin
Priority: normal Keywords:

Created on 2011-03-07 03:22 by victorywin, last changed 2011-03-07 05:47 by victorywin. This issue is now closed.

Messages (4)
msg130219 - (view) Author: Victor (victorywin) Date: 2011-03-07 03:22
Hi dear developers,
"Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32"
Hello,
Several days ago I downloaded Python 3.2, the standard windows installer, for windows vista. Here's the bug:
in the Python-command line, the following works:
"
>>>if 2==2:print('yes')
...else:print('n')
...
yes
"
Now, in the Python shell (IDLE-python gui, the standard one included with the installer of python), the only way it works is:
>>> if 2==2:print('yes')
else:print('n')

yes

So, it requires me to put "else" unindented. On the other hand, if the "if--else" statement is included in the definition of a function, then it requires that "else" be exactly under "if". Same is true when using "elif".
This was frustrating, when trying to learn "if-else" statement, because it took me half an hour of experimenting.
Shouldn't it be consistent?

Victor
msg130221 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-03-07 04:24
It is consistent: the else goes *always* under the if. If the if is unindented, so must be the else.
msg130223 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-07 04:43
In both cases, if-clause and the else-clause are at the beginning of the line (from Python's point of view).

The difference is that the Python command line interpreter adds a "..." to the beginning of secondary lines.  That makes the else-clause visually line-up with the if-clause which is prefixed with ">>>". 

With IDLE, the prefix for a secondary line is "", so the else-clause is still at the beginning of the line eventhough it doesn't visually line-up with the if-clause.
msg130226 - (view) Author: Victor (victorywin) Date: 2011-03-07 05:47
Thanks, people.
Now I understood that both "if" and "else" should be always at the beginning of the lines they are sitting on.
History
Date User Action Args
2011-03-07 05:47:25victorywinsetnosy: loewis, rhettinger, victorywin
messages: + msg130226
2011-03-07 04:43:39rhettingersetnosy: + rhettinger
messages: + msg130223
2011-03-07 04:25:14loewissetstatus: open -> closed
resolution: not a bug
2011-03-07 04:24:58loewissetnosy: + loewis
messages: + msg130221
2011-03-07 03:22:37victorywincreate