strange behaviour with while-else statement

Terry Reedy tjreedy at udel.edu
Mon Mar 7 15:41:28 EST 2011


On 3/7/2011 11:43 AM, Victor Paraschiv wrote:
> 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

Tabs do not survive in email/newsgroup posts. Bad idea to send.

In any case, the interactive interpreter and IDLE shell imitation 
thereof only parse one (1) statement at a time. You are trying to type 
two be removing the indent. When interpreted as one statement, the 
dedent is an error. However, above was wrong in original anyway.

>  >>> while True:
> s = input('Enter something : ')
> if s == 'quit':
> break
> print('Length of the string is', len(s))

Up to here, this version was correct, just hit return twice to run.
But I almost never type something so complex in the shell. I nearly 
always use editor, where I can correct mistakes easily.

-- 
Terry Jan Reedy




More information about the Python-list mailing list