gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:11:17 30 4
gpt4 key购买 nike

我在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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com