Python 3.2 print(end='') 错误
我是一名重生的业余编程新手,试图在 Linux 上使用 Geany 学习 Python 3 (3.2)。我一直在尝试修改 Swaroop CH 的 Python 3 教程 我的代码如下:
#!/usr/bin/env python3
# Filename: poem.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
with open('poem.txt', mode = 'w') as pfile:
pfile.write(poem)
with open('poem.txt', mode = 'r') as pfile:
while True:
line = pfile.readline()
if len(line) == 0:
break
print(line, end='')
我无法编译程序。我收到以下我不明白的错误:
SyntaxError: ('invalid syntax', ('poem.py', 19, 24, " print(line, end='')\n"))
在运行他的代码不变时,我收到相同的错误。一旦我删除 end=' '
,它就可以正常工作。如果我省略它,则会在诗的每一行之间打印一个空行。
我将不胜感激任何帮助/解释。
I'm a born again amateur programming novice trying to learn Python 3 (3.2) using Geany on Linux. I've been trying to rework the following example in Swaroop C H's Python 3 tutorial My code is as follows:
#!/usr/bin/env python3
# Filename: poem.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
with open('poem.txt', mode = 'w') as pfile:
pfile.write(poem)
with open('poem.txt', mode = 'r') as pfile:
while True:
line = pfile.readline()
if len(line) == 0:
break
print(line, end='')
I can't compile the program. I get the following error which I don't understand:
SyntaxError: ('invalid syntax', ('poem.py', 19, 24, " print(line, end='')\n"))
I get the same error when running his code unchanged. It works fine once I remove end=' '
. If I omit it a blank line is printed between every line of the poem.
I'd be grateful for any help/explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的文件包含 Python 3 的正确“shebang”行:
但是,为了使该 shebang 行生效,您必须直接运行脚本:(
您可能必须执行
chmod +x诗歌.py 使脚本可执行。)
如果您选择显式运行 Python 解释器:
那么运行的
python
将是您的 PATH 中的第一个解释器(类型python --version< /code> 看看是哪一个)。您仍然可以使用以下命令显式运行 Python 3:
这应该适合您。
Your file contains a correct "shebang" line for Python 3:
However, in order for this shebang line to take effect, you must run your script directly as:
(You may have to do
chmod +x poem.py
to make the script executable.)If you choose to explicitly run the Python interpreter:
then the
python
that gets run will be the first one in your PATH (typepython --version
to see which one that is). You can still run Python 3 explicitly with:which should work for you.
您没有安装 Python 3.x,或者没有使用它。这在 Python 3.x 下对我来说运行良好,但在 Python 2.x 下我收到了错误。 Shebangs 不是保证,无论您使用什么来运行脚本,都必须遵守它们,如果您直接运行解释器,它们将被忽略。因此请尝试确保您运行的是 Python3。根据您的环境,这可能会以不同的方式完成,但在 Unix 下,尝试使用
python3
而不是python
(尽管有些发行版如 Arch Linux 映射python 到 Python 3.x 和
python2
到 Python 2.x)。另一方面,所有文件在 Python 中都是可迭代的,所以你最好这样做:
另请注意 PEP8 建议:
就像你的模式参数一样。
You don't have Python 3.x installed, or are not using it. This runs fine for me under Python 3.x, but I get the error you have under Python 2.x. Shebangs are not a guarentee, they have to be honoured by whatever you are using to run the script, and if you run the interpreter directly, will be ignored. So try making sure you are running Python3. Depending on your environment, this might be done in different ways, but under Unix, try
python3
instead ofpython
(although some distros like Arch Linux mappython
to Python 3.x andpython2
to Python 2.x).On a different note, all files are iterables in Python, so you are much better off doing:
Also note PEP8 suggests:
As in your mode arguments.