Python行结束语法错误,结尾=''

时间:2014-03-12 11:45:40

标签: python

前几天我开始学习python。我有一个我从python docs复制的fibonacci函数。它在print语句中使用end =''

给出了语法错误

我手动重写了所有代码,但仍然收到错误

def fibonacci2(n):
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()    

它说syntax error while detecting tuple

提前致谢

编辑:对不起,我忘了写,我正在使用python 3。

2 个答案:

答案 0 :(得分:1)

Python 3.x中,您可以撰写print(b, end=' '),但您无法在print(b, end=' ')中撰写Python 2.x。因为Python 3.x print是函数,而Python 2.x print只是声明。

如果您想在end=''的print语句中使用Python 2.x,请使用from __future__ import print_function

答案 1 :(得分:0)

重要的是要记住在python&lt; V3.x print不是函数