Error creating Python object

Asked

Viewed 58 times

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 = Row (2, 8)

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

Where is the error ???

1 answer

3


The error is here:

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

You put _x, but it’s _y.

  • That’s right, I didn’t even see that mistake on the property.

  • @Thiagothiaguinho: Don’t forget to accept the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.