尝试创建新列表时出现语法错误

时间:2012-03-04 21:40:24

标签: python list syntax-error

只是尝试编写一个获取时钟指针角度的函数,但在尝试创建空列表时出现错误。它以前没有任何问题,已经在其他脚本上工作过。

def gethandpos():
    now=datetime.datetime.now()
    datetime.time(now.hour,now.minute,now.second)
    m=float(now.minute+now.second/60)
    h=float(now.hour+(m/60))
    hangle=math.fabs(((h*360)/12)-90)
    mangle=math.fabs(((m*360)/60)-90)
    sangle=(math.fabs((float((now.second*360)/60))-90)
    coords=[]
    coords.append((math.cos(math.radians(sangle)),math.sin(math.radians(sangle))))
    coords.append((math.cos(math.radians(mangle)),math.sin(math.radians(mangle))))
    coords.append((math.cos(math.radians(hangle)),math.sin(math.radians(hangle))))
    print coords

输出:

coords=[]
     ^
syntax error: invalid syntax

我做错了什么?

2 个答案:

答案 0 :(得分:3)

sangle=(math.fabs((float((now.second*360)/60))-90)
       1         23     45              1   23   4

答案 1 :(得分:1)

该行

sangle=(math.fabs((float((now.second*360)/60))-90)

在开头有一个额外的左括号。尝试

sangle = math.fabs(float(now.second * 360 / 60) - 90)

代替。

Python忽略括号内的换行符。这就是为什么以下行被解释为sangle赋值的一部分,导致语法错误。

我建议以更易读的方式格式化代码:在运算符周围使用空格,不要在不需要的地方使用太多括号,将复杂表达式分成多个步骤等。

相关问题