Python Type Error a float is required

Asked

Viewed 69 times

0

When I print C1.getTotalDesconts() and C1.getSalarioLike() for some reason python is requiring a conversion to float, these methods always return int or float, I can’t imagine the reason for this conversion.

I have tried using float() in the return of the methods and inside the print but the error continues if I use . format in print the program runs with this message instead of variables:

Total dos Descontos: <bound method calcPagamento.__calcularTotalDescontos of <__main__.calcPagamento object at 0x7f1574d18b00>
>                                                                                                                             
Sálario Líquido: <bound method calcPagamento.__calcularSalarioLiquido of <__main__.calcPagamento object at 0x7f1574d18b00>>  

The funny thing is that the virtually identical C1.getSalarioBruto() method works without any problem. I would like to understand what is happening.

class calcPagamento:
__salarioBruto = 0
__sindicato = 0
__fgts = 0
__ir = 0
__totalDescontos = 0.0
__salarioLiquido = 0.0



def calcularPagamento(self, pagamentoHora, horasTrabalhadas):
    try:
        self.__salarioBruto = self.__calcularSalarioBruto(pagamentoHora, horasTrabalhadas)
        self.__sindicato, self.__fgts, self.__ir = self.__calcularImpostos(self.__salarioBruto)
        self.__totalDescontos = self.__calcularTotalDescontos
        self.__salarioLiquido = self.__calcularSalarioLiquido
        return True
    except ValueError:
        print('Caught this error: ' + repr(ValueError))
        return False


def getSalarioBruto(self):
    return self.__salarioBruto

def getFgts(self):
    return self.__fgts

def getSindicato(self):
    return self.__sindicato

def getIr(self):
    return self.__ir

def getTotalDescontos(self):
    return self.__totalDescontos

def getSalarioLiquido(self):
    return self.__salarioLiquido

def __calcularSalarioBruto(self, pagamentoHora, horasTrabalhadas):
    if pagamentoHora < 0 or horasTrabalhadas < 0:
        raise ValueError("O Salário Bruto não pode ser negativo!")
    else:    
        return pagamentoHora * horasTrabalhadas

def __calcularImpostos(self, salarioBruto):
    sindicato = salarioBruto * 0.03
    fgts = salarioBruto * 0.11

    if salarioBruto < 900:
        ir = 0
    elif salarioBruto >= 900 and salarioBruto < 1500:
        ir = 0.05
    elif salarioBruto >= 1500 and salarioBruto < 2500:
        ir = 0.1
    else:
        ir = 0.2

    return sindicato, fgts, ir

def __calcularSalarioLiquido(self, salarioBruto, impostos):
    salarioLiquido = salarioBruto

    for x in range(0, len(impostos)):
        salarioLiquido = salarioLiquido - descontos[0]

    return salarioLiquido

def __calcularTotalDescontos(self, salarioBruto, impostos):

    for x in range(0, len(impostos)):
        descontos[x] = salarioBruto - salarioBruto * impostos[x]
        totalDescontos += descontos[x]
    return totalDescontos  

main:

valorHora = float(input("Informe o valor da hora trabalhada: "))
horasMes = int(input("Informe a quantidade de horas trabalhadas no mês: "))

c1 = calcPagamento()

if c1.calcularPagamento(valorHora, horasMes):

    print("Salário Bruto: %.2f" % c1.getSalarioBruto())
    print("Sindicato: {0:.1f}%".format(c1.getSindicato()))
    print("FGTS: {0:.1f}%".format(c1.getFgts()))
    print("Imposto de Renda: {0:.1f}%".format(c1.getIr()))
    print("Total dos Descontos: %.2f" % c1.getTotalDescontos())
    print("Sálario Líquido: %.2f" % c1.getSalarioLiquido())
else:
    print("falhou")

1 answer

1


On line 16 there is no parenthesis in the method

#Incorreto:
self.__totalDescontos = self.__calcularTotalDescontos
#correto
self.__totalImpostos = self.__sindicato + self.__fgts + self.__ir #Só adicionei essa linha pra ficar mais legivel
self.__totalDescontos = self.__calcularTotalDescontos(self.__salarioBruto, self.__totalImpostos )

in the first way you are passing the reference of the function to the self. __totalDiscounts and thus transforming it into a function too:

self.__totalDescontos = self.__calcularTotalDescontos

print(self.__calcularTotalDescontos(self.__salarioBruto, self.__totalImpostos ) == self.__totalDescontos(self.__salarioBruto, self.__totalImpostos ))

Something else! Instead of you using the "gets" (getSalarioBruto(), etc) you can simply put the @Property decorator in the function and call it as method:

class calcPagamento:

    __salarioBruto = 7

    @property
    def salarioBruto(self):
        return self.__salarioBruto

c1 = calcPagamento()

print(c1.salarioBruto)

  • Even childish error, blind people thing, going from typed to typed language is very problematic. Solved my problem, if you want to make your answer more complete explain the difference between getters/setters with @Property linking a post

Browser other questions tagged

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