Python course example

class Line: def __init__(self, (x, y), (x1, y1)): self._b = (x, y) self._e = (x1, y1) def length(self): import math return math.sqrt((self._b[0]-self._e[0])**2 + (self._b[1]-self._e[1])**2) 

I have Python 3.4. In line 2 gives the error Invalid sintax.

1 answer 1

This syntax is incorrect for Python 3.
The correct code is:

 class Line: def __init__(self, first, second): self._b = first # записали в self._b кортеж (1, 2) self._e = second # записали в self._e кортеж (3, 4) def length(self): import math return math.sqrt((self._b[0]-self._e[0])**2 + (self._b[1]-self._e[1])**2) line = Line((1, 2), (3, 4)) 

The class takes two arguments: first and second - we pass two arguments to it, each of which is a tuple with two elements.

  • one
    The answer is partially incorrect, in Python 2.7 the code from the question works fine without any changes - andreymal
  • one
    But they wrote that they use Python 3.4 - NikitaZakharov
  • This is the author, but what about it is written on the intuit itself, no one began to tell us) - andreymal