I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error I am using python 2.7 I am coding from vim editor and here is my code:
我刚开始用python编程,我试着用分隔符和结尾打印出来,但是它仍然给我一个语法错误我用的是python 2.7我用的是vim编辑器,这是我的代码:
import sys, os, time
from __future__ import print_function
for x in range(0,10):
print x, sep=' ', end=''
time.sleep(1)
please, help. And using my code, show me where I should actually use the from __future__ import print_function
Thank you
请帮助。使用我的代码,告诉我应该在哪里使用from __future__导入print_function谢谢
1 个解决方案
#1
115
First of all, from __future__ import print_function
needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print
as a function now. That's the whole point of from __future__ import print_function
; to bring the print
function from Python 3 into Python 2.6+.
首先,从__future__导入print_function需要成为脚本中的第一行代码(除了下面提到的一些例外)。其次,正如其他答案所言,您现在必须使用print作为函数。这就是从__future__ import print_function的全部意义;将Python 3中的打印函数转换为Python 2.6+。
from __future__ import print_function
import sys, os, time
for x in range(0,10):
print(x, sep=' ', end='') # No need for sep here, but okay :)
time.sleep(1)
__future__
statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:
__future__语句需要靠近文件的顶部,因为它们更改了语言的基本内容,因此编译器需要从一开始就了解它们。从文档:
A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.
将来的语句会在编译时得到特别的识别和处理:对核心构造语义的更改通常通过生成不同的代码来实现。甚至可能是新特性引入了新的不兼容语法(例如新的保留词),在这种情况下,编译器可能需要对模块进行不同的解析。这样的决定不能推迟到运行时。
The documentation also mentions that the only things that can precede a __future__
statement are the module docstring, comments, blank lines, and other future statements.
该文档还提到,__future__语句之前只能有模块docstring、注释、空行和其他未来语句。
#1
115
First of all, from __future__ import print_function
needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print
as a function now. That's the whole point of from __future__ import print_function
; to bring the print
function from Python 3 into Python 2.6+.
首先,从__future__导入print_function需要成为脚本中的第一行代码(除了下面提到的一些例外)。其次,正如其他答案所言,您现在必须使用print作为函数。这就是从__future__ import print_function的全部意义;将Python 3中的打印函数转换为Python 2.6+。
from __future__ import print_function
import sys, os, time
for x in range(0,10):
print(x, sep=' ', end='') # No need for sep here, but okay :)
time.sleep(1)
__future__
statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:
__future__语句需要靠近文件的顶部,因为它们更改了语言的基本内容,因此编译器需要从一开始就了解它们。从文档:
A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.
将来的语句会在编译时得到特别的识别和处理:对核心构造语义的更改通常通过生成不同的代码来实现。甚至可能是新特性引入了新的不兼容语法(例如新的保留词),在这种情况下,编译器可能需要对模块进行不同的解析。这样的决定不能推迟到运行时。
The documentation also mentions that the only things that can precede a __future__
statement are the module docstring, comments, blank lines, and other future statements.
该文档还提到,__future__语句之前只能有模块docstring、注释、空行和其他未来语句。