简体   繁体   English

SyntaxError:意外的令牌“打印”

[英]SyntaxError: unexpected token 'print'

The following code runs fine in IDLE [2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]]: 以下代码在IDLE [2.7.8(默认值,2014年6月30日,16:03:49)[MSC v.1500 32位(Intel)]]中运行良好:

x = 5
if True:
   pass
   x=6
else:
   x=7
print x

But in IronPython ['2.7.3 (IronPython 2.7.3 (2.7.0.40) on .NET 4.0.30319.34014 (64-bit))], the last line gives a SyntaxError: unexpected token 'print' 但是在IronPython [[2.7.3(.NET 4.0.30319.34014(64位)上的IronPython 2.7.3(2.7.0.40))]中,最后一行给出了SyntaxError: unexpected token 'print'

I copied and pasted the same lines of text in both systems, so they should be the same, including any invisible characters. 我在两个系统中都复制并粘贴了相同的文本行,因此它们应该是相同的,包括任何不可见的字符。

What could be the cause? 可能是什么原因?

If you have it on a Microsoft Visual Studio, then it seems that you have to have any print commands in () . 如果您在Microsoft Visual Studio上安装它,则似乎必须在()具有任何print命令。 So, for example: 因此,例如:

print (dictionary_name['notreal'])

or 要么

print (x)

This is a quirk of the interactive interpreter, and has nothing to do with Ironpython vs regular Python. 这是交互式解释器的一个怪癖,与Ironpython和常规Python无关。 If you save your code in a file and run it, it will work with any Python. 如果将代码保存在文件中并运行,那么它将适用于任何Python。

Here is a shorter example that shows the error: 这是显示错误的较短示例:

>>> if False:
...      pass
... pass
  File "<stdin>", line 3
    pass
       ^
SyntaxError: invalid syntax

When using the interactive interpreter, you must end a multi-line statement with an empty line, as so: 使用交互式解释器时,必须以空行结束多行语句,如下所示:

>>> if False:
...     pass
... 
>>> pass
>>> 

If you're doing python in VS code, always remind yourself to put bracket for print command. 如果您在VS代码中使用python,请始终提醒自己在括号中添加print命令。 print(). 打印()。 It should be worked! 应该可以的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM