为什么我在 Python 解释器中收到语法错误?

发布于 2024-08-27 05:29:10 字数 297 浏览 6 评论 0原文

当我从 .py 文件中尝试时,此代码可以工作,但在命令行解释器和空闲中失败。

>>> try:
...     fsock = open("/bla")
... except IOError:
...     print "Caught"
... print "continue"
  File "<stdin>", line 5
    print "continue"
        ^
SyntaxError: invalid syntax

我正在使用 python 2.6

This code works when I try it from a .py file, but fails in the command line interpreter and Idle.

>>> try:
...     fsock = open("/bla")
... except IOError:
...     print "Caught"
... print "continue"
  File "<stdin>", line 5
    print "continue"
        ^
SyntaxError: invalid syntax

I'm using python 2.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短叹 2024-09-03 05:29:10

对于 Python 3,print 是一个函数而不是语句,因此如果您使用 Python,则需要在参数两边加上括号,如 print("continue") 中所示3.

然而,插入符号指向的位置比 Python 3 更早,因此您必须使用 Python 2.x。在这种情况下,错误是因为您在交互式解释器中输入此内容,并且它需要一些“帮助”来弄清楚您试图告诉它的内容。在上一个块之后输入一个空行,以便它可以正确解读缩进,如下所示:

>>> try:
...     fsock = open("/bla")
... except IOError:
...     print "Caught"
...
(some output shows here)
>>> print "continue"

With Python 3, print is a function and not a statement, so you would need parentheses around the arguments, as in print("continue"), if you were using Python 3.

The caret, however, is pointing to an earlier position than it would be with Python 3, so you must be using Python 2.x instead. In this case, the error is because you are entering this in the interactive interpreter, and it needs a little "help" to figure out what you are trying to tell it. Enter a blank line after the previous block, so that it can decipher the indentation properly, as in this:

>>> try:
...     fsock = open("/bla")
... except IOError:
...     print "Caught"
...
(some output shows here)
>>> print "continue"
余罪 2024-09-03 05:29:10

您需要保留一个空行来关闭 except 块。 ... 表示它仍在尝试将代码放入该块中,即使您缩进了它。这只是交互式解释器的一个怪癖。

You need to leave a blank line to close the except block. The ... indicates it is still trying to put code in that block, even though you dedent it. This is just a quirk of the interactive interpreter.

蔚蓝源自深海 2024-09-03 05:29:10

在解释器中试试这个:

try:
    fsock = open("/bla")
except IOError:
    print "Caught"

print "continue"

这里重要的是缩进后的空行。我正在使用 python 2.6 解释器,它会抛出与您相同的语法错误。

这是因为解释器期望由空行分隔的单个块。另外,空行(两个新行字符)指示块的结尾并且解释器应该执行它。

Try this one in the interpreter:

try:
    fsock = open("/bla")
except IOError:
    print "Caught"

print "continue"

Important here is the empty line after the indentation. I'm using the python 2.6 interpreter and it throws the same Syntax error as you.

This is because the interpreter expects single blocks separated by blank lines. Additionally the blank line (two new line characters) indicates the end of the block and that the interpreter should execute it.

梦里的微风 2024-09-03 05:29:10

与 if/else 或 for 或 while 一样,try/ except 是复合语句。在Python shell提示符中,控制关键字:...后面的语句需要缩进,因为该语句或复合语句是一条一条执行的。您的最后一个 print("continue") 与顶级 try: 对齐,并被视为另一个语句,因此存在语法错误。
如果你想以交互方式测试 try/ except/else/finally ,你可以将它们包装在一个函数中:

>>> def t():
...     try:
...             print(x)
...     except:
...             print('exception')
...             return
...     finally:
...             print('finally')
...     print('continue')
...     print('end function t()')
...
>>> t()
exception
finally
>>> x = 99
>>> t()
99
finally
continue
end function t()
>>>

这是使用 Windows python shell 完成的。当我尝试使用 PyCharm IDE python 控制台时,它允许在复合语句之后再执行一条语句。

>>> n = 0
>>> while n!= 5:
...    n+=1
... print('n = {}'.format(n)) 
n = 5   # no syntax error
>>>

Like if/else or for or while, try/except is a compound statement. In a Python shell prompt, statements after control keywords: ... need to be indented because the statement or compound statement is executed one by one. Your last print("continue") is aligned with the top-level try: and considered another statement hence syntax error.
If you want to test out try/except/else/finally interactively, you can wrap them in a function:

>>> def t():
...     try:
...             print(x)
...     except:
...             print('exception')
...             return
...     finally:
...             print('finally')
...     print('continue')
...     print('end function t()')
...
>>> t()
exception
finally
>>> x = 99
>>> t()
99
finally
continue
end function t()
>>>

This was done with Windows python shell. When I tried on PyCharm IDE python console, it allows one more statement after the compound statement to execute.

>>> n = 0
>>> while n!= 5:
...    n+=1
... print('n = {}'.format(n)) 
n = 5   # no syntax error
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文