2011-04-24 53 views
2

我在python交互模式下嘗試編碼像波紋管時得到了語法錯誤反應。Python交互模式下的錯誤?

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

但它保存爲腳本時正常執行。

~ $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 

它是一個錯誤?或者我應該知道的有關python交互模式的其他任何事情?

~ $python -V 
Python 2.6.6 
+0

[過了一會兒做蟒紋]的可能重複(http://stackoverflow.com/questions/5751135/python-print-done-after-while) – 2011-04-24 00:47:38

回答

2

我想當你回到第一列縮進時,這個列必須留空它表明你打開的區塊現在已經準備好被解釋了。

如果你把它放在一個函數中,在它正常工作後調用它。

In [66]: def fun(): 
    ....:  while True: 
    ....:   reply = raw_input("enter text:") 
    ....:   if reply == 'stop': 
    ....:    break 
    ....:   print reply 
    ....:  print "bye" 
    ....: 
+0

是不是意味着每塊返回到第一列現在已準備好以交互模式進行解釋?得到它了。謝謝。 – 2011-04-24 01:36:56