1

i got the syntax error reaction while try coding like bellow in python interactive mode.

>>> while True:
...   reply = raw_input('enter text:')
...   if reply == 'stop':
...     break
...   print reply
... print 'bye'
  File "<stdin>", line 6
    print reply
        ^
SyntaxError: invalid syntax
>>>

but it executed normally if save as script.

~ $cat test.py
#!/usr/bin/env python
# encoding=utf8

while True:
  reply = raw_input('enter text:')
  if reply == 'stop':
    break
  print reply
print 'bye'
~ $python test.py
enter text:19
19
enter text:456789
456789
enter text:$%^&*(
$%^&*(
enter text:TGHJKLO:P
TGHJKLO:P
enter text:#$%^&*()_
#$%^&*()_
enter text:stop
bye

is it a bug? or any other things i should know about python interactive mode?

~ $python -V
Python 2.6.6
2

1 Answer 1

2

I think when you return to the first column of indentation this must be left empty its to indicate that the block you opened is now ready to be interpreted.

If you put this in a function an invoke it after it works properly.

    In [66]: def fun():
   ....:     while True:
   ....:         reply = raw_input("enter text:")
   ....:         if reply == 'stop':
   ....:             break
   ....:         print reply
   ....:     print "bye"
   ....: 
Sign up to request clarification or add additional context in comments.

1 Comment

Is that mean every blocks return to the first column is now ready to be interpreted in interactive mode? Got it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.