i got the syntax error reaction while try coding like bellow in开发者_如何学Python 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
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"
....:
精彩评论