According to the documentation, method sort
sorts the list in-place (that is, modifies the list itself) and it is also said that "it does not Return the Sorted Sequence" (does not return the ordered list).
Moreover, the documentation also says that:
The methods that add, subtract, or rearrange their Members in place, and don’t Return a specific item, Never Return the Collection instance itself but None
.
That is, methods such as sort
, that change the content in place and do not return to their own list, return None
.
So in your case the variable lados
was with the value None
, and when trying to obtain an element of it (with lados[0]
), makes the mistake of Nonetype Object is not subscriptable.
One way to solve is to first create the list and then sort it:
lados = [self.a, self.b, self.c]
lados.sort()
# etc...
Or else use sorted
, which returns the ordered list:
lados = sorted([self.a, self.b, self.c])
# etc...
Another way is, instead of creating the list lados
, use multiple assignment:
def retangulo(self):
a, b, c = sorted([self.a, self.b, self.c])
return a ** 2 + b ** 2 == c ** 2
Thus, the variables a
, b
and c
shall receive respectively the first, second and third items of the orderly list which sorted
returns (remember that they are local to the function and are not confused with self.a
, self.b
and self.c
). And in calculus the parentheses are unnecessary, since exponentiation takes precedence greater than the sum.
Also note that I can directly return the result of the comparison, since the result of a comparison with ==
is a boolean (that is, it is True
or False
). In general, any expression of the type:
if condicao:
return True
return False
Can be summarized to:
return condicao
Like condicao
in your case is a boolean, you can return it directly. It would only make a difference if condicao
was not boolean, since in Python any object can be used in a context boolean (and then the object itself would be returned instead of True
or False
), but as it is the result of a comparison, you can return it directly.
About the error you got in the terminal, I think it’s a typo, because what you typed is:
tri1.b ** 2 + tri1.c ** 2 = tri1.a ** 2
Since you should wear ==
instead of =
. Only one equal sign is the assignment operator, which makes no sense in the above context. Switching to ==
, would work smoothly.