Home > Enterprise >  NameError: name 'a' is not defined pycharm
NameError: name 'a' is not defined pycharm

Time:04-21

class Triang:
    def __init__(self, a, b, c):
        self.valido = None
        self.classificacao_lado = None  # Equilátero, Isósceles, Escaleno
        self.classificacao_angulo = None  # Retângulo, Obtusângulo, Acutângulo

    def istriang(self):
        if a >= b c or b >= a c or c >= a b:
            self.valido = False
        self.valido = True

    def classificar_lado(self):
        if a == b == c:
            self.classificacao_lado = 'equilatero'
        if (a == b and a != c) or (a == c and a != b) or (b == c and b != a):
            self.classificacao_lado = 'isosceles'
        else:
            self.classificacao_lado = 'escaleno'

    def classificar_angulo(self):
        if a**2 == b**2   c**2:
            self.classificacao_angulo = 'retangulo'
        if a**2 > b**2   c**2:
            self.classificacao_angulo = 'obtusangulo'
        if a ** 2 < b ** 2   c ** 2:
            self.classificacao_angulo = 'acutangulo'


A, B, C = [float(item) for item in input().split()]  # 5 5 5
triangulo = Triang(A, B, C)
triangulo.classificar_lado()
triangulo.classificar_angulo()
print(triangulo.classificacao_lado)
print(triangulo.classificacao_angulo)

I'm creating a triangle class, but when doing the comparisons with the sides (a, b, c) something is going wrong and I don't understand.

NameError: name 'a' is not defined

why am i getting this error? obs: "a", "b", "c" are the sides of my triangle

CodePudding user response:

Your __init__ method needs to store a, b, and c as attributes of self:

def __init__(self, a, b, c):
    self.valido = None
    self.classificacao_lado = None  # Equilátero, Isósceles, Escaleno
    self.classificacao_angulo = None  # Retângulo, Obtusângulo, Acutângulo
    self.a, self.b, self.c = a, b, c

And then use self.a etc. to reference these values in your other methods.

def istriang(self):
    if self.a >= self.b   self.c or self.b >= self.a   self.c or self.c >= self.a   self.bb:
        self.valido = False
    else:
        self.valido = True

(there are other problems in this method, such as a missing else statement... I have taken the liberty of adding one)

Similar changes are needed in the other methods to refer to the sides of the triangle as attributes of the instance.

CodePudding user response:

Try this:

class Triang:
    def __init__(self, a, b, c):
        self.valido = None        
        self.a = a
        self.b = b
        self.c = c

    def istriang(self):
        if self.a >= self.b self.c or self.b >= self.a self.c or self.c >= self.a self.b:
            self.valido = False
        self.valido = True

    def classificar_lado(self):
        if self.a == self.b == self.c:
            return 'equilatero'
        if (self.a == self.b and self.a != self.c) or (self.a == self.c and self.a != self.b) or (self.b == self.c and self.b != self.a):
            return 'isosceles'
        else:
            return 'escaleno'

    def classificar_angulo(self):
        if self.a**2 == self.b**2   self.c**2:
            return 'retangulo'
        if self.a**2 > self.b**2   self.c**2:
            return  'obtusangulo'
        if self.a ** 2 < self.b ** 2   self.c ** 2:
            return 'acutangulo'

A, B, C = [float(item) for item in input().split()]  # 5 5 5
triangulo = Triang(A, B, C)
lado = triangulo.classificar_lado()
angulo = triangulo.classificar_angulo()
print(lado)
print(angulo)

CodePudding user response:

Can I suggest a different way? Having all those selfs in the code makes it very hard to read plus tedious to write and to change.

You could instead have istriang, classificar_lado and classificar_angulo as regular free functions (not associated with any class), pass a, b, and c into the functions and return the result.

Here is the code I have suggested. You may notice that its simpler to read and your functions are more generally useful, as they are not associated with any class.

def istriang(a, b, c):
    if a >= b c or b >= a c or c >= a b:
        return False
    return True


def classificar_lado(a, b, c):
    if a == b == c:
        return 'equilatero'
    if (a == b and a != c) or (a == c and a != b) or (b == c and b != a):
        return 'isosceles'
    else:
        return 'escaleno'


def classificar_angulo(a, b, c):
    if a**2 == b**2   c**2:
        return 'retangulo'
    if a**2 > b**2   c**2:
        return 'obtusangulo'
    if a ** 2 < b ** 2   c ** 2:
        return 'acutangulo'
    else:
        return '?'


class Triang:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        self.valido = istriang(a, b, c)
        self.classificacao_lado = classificar_lado(a, b, c)
        self.classificacao_angulo = classificar_angulo(a, b, c)


A, B, C = [float(item) for item in input().split()]  # 5 5 5
triangulo = Triang(A, B, C)
print(triangulo.classificacao_lado)
print(triangulo.classificacao_angulo)

Also your function classificar_angulo does not handle all possible inputs. It needs an else statement or to return some default value if none of the if conditions above are met. I have just changed it to return '?'. You may have a better idea about what it should return.

  • Related