strange behaviour with while-else statement

Victor Paraschiv victorywin5 at yahoo.com
Mon Mar 7 11:43:03 EST 2011


Hi and please help me understand if it is a bug, or..,as someone said, there's a 'bug' in my understanding:
(Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32) (windows vista, the regular windows python installer)
It's about the following code: 
while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')

when I put it in a editor, and run it from there ("Run module F5"), 
it runs fine; but when I try to type it in the python shell (IDLE), or in the 
 python command line, it gives errors, though I tried to edit it differently:
>>> while True:
        s = input('Enter something : ')
        if s == 'quit':
            break
        print('Length of the string is', len(s))
print('Done')
SyntaxError: invalid syntax
>>> while True:
            s = input('Enter something : ')
            if s == 'quit':
                break
            print('Length of the string is', len(s))
        print('Done')
SyntaxError: unindent does not match any outer indentation level
>>> 

The only way I noticed it would accept is to set the last "print" statement directly under/in the block of "while": which is not the intention. (According to the docs, while statement should work without the "else" option). Is this a bug?
Thanks, Victor
p.s. the example is taken from http://swaroopch.com/notes/Python_en:Control_Flow






      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110307/37c8e3ed/attachment.html>


More information about the Python-list mailing list