我是一個天生的業餘編程新手,嘗試在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