简体   繁体   中英

syntax error in trying to create new list

Just trying to write a function that gets clock hand angles, but getting an error where i try to create an empty list. It has worked on other scripts previously without any issues.

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

output:

coords=[]
     ^
syntax error: invalid syntax

what am I doing wrong?

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

The line

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

has an additional opening parenthesis in the beginning. Try

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

instead.

Python ignores linebreaks inside parentheses. That's why the following line was interpreted as part of the assignment to sangle , leading to a syntax error.

I recommend to format your code in a more readable way: use spaces around operators, don't use too many parentheses where they are unnecessary, split complex expression into multiple steps etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM