SyntaxError: invalid syntax python, what's the cause?

I tried this

for i in range(len(current_pos)):
  vector.append(to_pos[i] - current_pos[i])

distance = math.sqrt((vector[0]2)+(vector[1]2))
angle = math.degrees(math.atan(vector[1]/vector[0]))

print(vector[1], vector[0])

SyntaxError: invalid syntax

What is the cause of this syntax error? Now I just write because I need more details.

2 answers

  • answered 2021-05-05 11:01 guettli

    This is the error:

    vector[0]2
    

    What should above code do?


    Do you want this?

    guettli@yoga15:~$ python
    Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> 2**4
    
    16
    

  • answered 2021-05-05 11:15 Mostafa Ayaz

    The power operation is done with **. So modify your code as

    for i in range(len(current_pos)):
      vector.append(to_pos[i] - current_pos[i])
    
    distance = math.sqrt((vector[0]**2)+(vector[1]**2))
    angle = math.degrees(math.atan(vector[1]/vector[0]))
    
    print(vector[1], vector[0])