python - python 交互模式中的错误?

标签 python interactive

我在Python交互模式下尝试像下面这样编码时得到了语法错误 react 。

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

最佳答案

我认为当您返回到缩进的第一列时,必须将其留空,以表明您打开的 block 现在已准备好进行解释。

如果您将其放入函数中,请在其正常工作后调用它。

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

关于python - python 交互模式中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5767910/

相关文章:

layout - 有没有好的交互式 3D 图形库?

javascript - 使用 document.getElementByID 访问动态生成的 DIV

python - 交互式 python matplotlib

python - bool() 和 operator.truth() 有什么区别?

python - 属性错误 : 'module' object has no attribute 'SystemSettings_GetFont'

Python influxdb 时间戳值错误

python - 我如何知道我的代码是部署在 GAE 上运行还是在本地运行?

python - python numpy 中的错误

javascript - 使用 Javascript 在遍历单位圆时绘制正弦波

javascript - 使用 JavaScript 使按钮使用react(添加动画)