所以我正在学习Python,我在Pycharm IDE上编写代码。我试图写一个函数,但由于某种原因,Pycharm不会接受'end ='语句,说它没有定义。
这是我的代码
def fib(n):
a,b=1,0
while a < n:
print (a, end=' ')
a,b=b,b+a
fib(1000)
它给了我以下错误:
line 7
print (a, end=' ')
^
SyntaxError: invalid syntax
它只在Pycharm上执行此操作。当我在dafult Python IDLE上尝试它时它工作得很好。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:3)
正如评论中已经回答的那样,只有python3具有print()
的关键字参数:
quaternion permquat;
permquat.x = 0;
permquat.y = 0;
permquat.z = 0;
permquat.w = 1;
quaternion permquat2;
permquat2.x = 0;
permquat2.y = 0;
permquat2.z = 0;
permquat2.w = 1;
quaternion local_rotation;
local_rotation.w = cosf( beta/(2.0*180.0));
local_rotation.x = 1.0 * sinf( beta/(2.0*180.0) );
local_rotation.y = 0.0 * sinf( beta/(2.0*180.0) );
local_rotation.z = 0.0 * sinf( beta/(2.0*180.0) );
quaternion local_rotation2;
local_rotation2.w = cosf( gamma/(2.0*180.0));
local_rotation2.x = 0.0 * sinf( gamma/(2.0*180.0));
local_rotation2.y = 1.0 * sinf( gamma/(2.0*180.0));
local_rotation2.z = 0.0 * sinf( gamma/(2.0*180.0));
permquat = mult(local_rotation, permquat);
normalize(permquat);
permquat2 = mult(local_rotation2, permquat2);
normalize(permquat);
matTransl3D = matrix(permquat);
glMultMatrixf(*matTransl3D);
matTransl3D = matrix(permquat2);
glMultMatrixf(*matTransl3D);
glTranslatef(-9, -9, -9); // bottom left
drawcube();
glPopMatrix();
对于python2,print
是一个定义为此的语句,没有关键字参数:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
您可以切换到python3或禁用该语句,并使用print()
功能将此未来行放在模块顶部:
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
from __future__ import print_function