Python 语法错误:无效语法 end=''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20073639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python SyntaxError: invalid syntax end=''
提问by Jonny
I am studying the book "Head First Python" and I'm having trouble this code:
我正在学习“Head First Python”这本书,但在这段代码中遇到了问题:
data = open('sketch.txt')
for each_line in data:
(role, line_spoken) = each_line.split(':')
print(role, end='')
print(' said: ', end='')
print(line_spoken, end='')
data.close()
Error:
错误:
File "Aula 3.py", line 12
print(role, end='')
^
SyntaxError: invalid syntax
sketch.txt:
草图.txt:
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
I'm two days trying to figure out why the code is not working. The error is always shown in "end =''".
我有两天的时间试图找出代码不起作用的原因。错误总是显示在“end=''”中。
采纳答案by falsetru
It seems like you're using Python 2.x, not Python 3.x.
看起来您使用的是 Python 2.x,而不是 Python 3.x。
Check your python version:
检查您的python版本:
>>> import sys
>>> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
>>> print(1, end='')
File "<stdin>", line 1
print(1, end='')
^
SyntaxError: invalid syntax
In Python 3.x, it should not raise Syntax Error:
在 Python 3.x 中,它不应该引发语法错误:
>>> import sys
>>> sys.version
'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]'
>>> print(1, end='')
1>>>
回答by Teddy
>>> import sys
>>> print(sys.version)
2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]
>>> from __future__ import print_function
>>> print(1, end=',')
1,
回答by kajal siyat
when you use eclipse editor,just typed like this,
当你使用eclipse编辑器时,就像这样输入,
from __future__ import print_function
for i in range(0,5):
for j in range(0,i):
print(j, end='')
print()
回答by Chris Adams
If you're running it from the command line you may also just need to use the python3 command instead of just python command such as:
如果您从命令行运行它,您可能还只需要使用 python3 命令,而不仅仅是 python 命令,例如:
python3 MyFile.py
回答by user8143631
If you are sure you have Python 3+ intalled, type this as first line in your .py file:
如果您确定安装了 Python 3+,请在 .py 文件的第一行输入以下内容:
#!/usr/bin/python3.x <<----- where x is your installed version.
Example: I start the file with: #!/usr/bin/python3.5(because I have 3.5 installed)
示例:我用以下方式启动文件:(#!/usr/bin/python3.5因为我安装了 3.5)
回答by John G.
I was having the same problem with Eclipse and IntelliJ IDEA, just solved it on Eclipse. Preferences with PyDev--interpreter had added Python 3.6 but the native Python on my Mac OS X was above the new install of the Python 3.6. So simply moving up the 3.6 in the interpreter solved my problem.
我在 Eclipse 和 IntelliJ IDEA 上遇到了同样的问题,刚刚在 Eclipse 上解决了它。PyDev--interpreter 的首选项添加了 Python 3.6,但我的 Mac OS X 上的本机 Python 高于新安装的 Python 3.6。因此,只需在解释器中向上移动 3.6 即可解决我的问题。



