Error in creating Python object

1
class Linha(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, valor):
        valor = int(valor)
        if valor >= 0:
            self._x = valor
        else:
            raise ValueError('Valor de x inválido !')

    @property
    def y(self):
        return self._x

    @y.setter
    def y(self, valor):
        valor = int(valor)
        if valor >= 0:
            self._y = valor
        else:
            raise ValueError('Valor de y inválido !')

    def __str__(self):
        return '(%i - %i)' %(self.x, self.y)

def main():
    while True:
        try:
            x = input('X :')
            y = input('Y :')
            linha = Linha(x, y)
            print(linha)
        except ValueError as erro:
            print(erro)

if __name__ == '__main__':
    main()

When I try to create an object for example:

line = Line (2, 8)

It creates a line object with values x = 2 and y = 2.

Where is the error ???

    
asked by anonymous 12.11.2017 / 19:04

1 answer

3

The error is here:

@property
def y(self):
    return self._x

You have put _x, but it is _y.

    
12.11.2017 / 23:24