我是一个天生的业余编程新手,尝试在Linux上使用Geany学习Python 3(3.2)。我一直在试图返工Swaroop C H's Python 3 tutorial下面的例子中我的代码如下:Python 3.2 print(end ='')error
#!/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=' '
,它工作正常。如果我忽略它,那么在诗的每一行之间都会打印一条空行。
我会很感激任何帮助/解释。
你确定你在使用Python 3吗?你有没有试过把它作为'python3 poem.py'来运行? – 2012-02-11 19:02:25
顺便说一下,本教程的作者似乎是一个新手(以及一个可怕的诗人:p)。迭代遍历文件行的Pythonic方法是“for pfile in line”。 – 2012-02-11 19:03:46