Problem To Overwrite Value

Asked

Viewed 38 times

0

I’m not able to overwrite the self.Valorbase on the Low Income class. Could someone help me?

te = 0.25588
tusd = 0.25971
icms = 0
desc_te = te - (te * 0.10 / 100)
desc_tusd = tusd - (tusd * 0.30 / 100)

class Cliente:
    def __init__(self, vb):
        self.ValorBase = vb * (te + tusd)
        self.NomeClasse = self.__class__.__name__

        def valorIcms(self):
            return self.ValorBase * icms

        print('{:^40}'.format(self.NomeClasse))
        print(vb)
        print(f'{self.ValorBase:.2f}')
        print(f'{self.valorIcms():.2f}')

class BaixaRenda(Cliente):
    def __init__(self, vb):
        Cliente.__init__(self, vb)
    
        if vb <= 220:
            self.ValorBase = vb * (desc_te + desc_tusd)

    def valorIcms(self):
        return self.ValorBase * icms

#Programa Principal
kwh = int(input('Valor Gasto: '))
b = BaixaRenda(kwh)
  • 1

    yes, it overwrites, but to see the new value has to do a new print, and I wanted the new value to appear in the print that is in the client class

  • It worked. Thank you so much for your help.

1 answer

1


I made some modifications to the code:

  • I have mastered the local function valorIcms() to become a base class method Cliente, because the way it was declared was a local function belonging to the scope of the class constructor.
  • I created a method to print a report by taking advantage of a few loose builtin print calls in the class Cliente.
  • I removed the method override valorIcms() because the superscription added nothing.

Example:

te = 0.25588
tusd = 0.25971
icms = 0
desc_te = te - (te * 0.10 / 100)
desc_tusd = tusd - (tusd * 0.30 / 100)

class Cliente:
    def __init__(self, vb):
        self.ValorBase = vb * (te + tusd)
        self.NomeClasse = self.__class__.__name__

    #Identei a função local para que se tornasse um método da classe base.
    def valorIcms(self):
        return self.ValorBase * icms
        
    #Criei um método para printar um relatório
    def relatorio(self):
        print('{:^40}'.format(self.NomeClasse))
        print(f'Valor base: {self.ValorBase:.2f}')
        print(f'ICMS: {self.valorIcms():.2f}')

class BaixaRenda(Cliente):
    def __init__(self, vb):
        Cliente.__init__(self, vb)
    
        if vb <= 220:
            self.ValorBase = vb * (desc_te + desc_tusd)    
    
    #Removi a sobrescrição do método valorIcms

kwh = int(input('Valor Gasto: '))
b = BaixaRenda(kwh)
b.relatorio()  #Imprime o relatório

Test the code on COLAB

Browser other questions tagged

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