I can’t find the code error

Asked

Viewed 360 times

-5

Could someone point out to me the error of this code:

valor_hora = float(input('Valor da hora trabalhada: '))
qtd_horas_trabalhadas = int(input('Quantidade de horas trabalahadas: '))

salario_bruto = valor_hora * qtd_horas_trabalhadas
ir = salario_bruto * 0.11
inss = salario_bruto * 0.08
sindicato = salario_bruto * 0.05
salario_liquido = salario_bruto - (ir + inss + sindicato)

print(f'''+ Salário Bruto : R$ {salario_bruto:.2f}
          - IR (11%) : R$ {ir:2.f}
          - INSS (8%) : R$ {inss:2.f}
          - SINDICATO (5%) : R$ {sindicato:.2f}
          ----------------------------
          = Salário líquido : R${salario_liquido:.2f}''')

Traceback (Most recent call last): print(f'''+ Gross Salary : R$ {salario_bruto} Valueerror: Format specifier Missing Precision

3 answers

5


The error is in:

- IR (11%) : R$ {ir:2.f}

and:

- INSS (8%) : R$ {inss:2.f}

If you ran separately you would notice which failed:

print(f'''+ Salário Bruto : R$ {salario_bruto:.2f}''')
print(f'''- IR (11%) : R$ {ir:2.f}''')
print(f'''- INSS (8%) : R$ {inss:2.f}''')
print(f'''- SINDICATO (5%) : R$ {sindicato:.2f}''')
print('----------------------------')
print(f'''= Salário líquido : R${salario_liquido:.2f}''')

Would get this:

    print(f'''- IR (11%) : R$ {ir:2.f}''')
ValueError: Format specifier missing precision

Namely the 2.f probably should be .2f

  • 1

    That’s right... lack of attention.

  • @Fabriciopaiva the hint I leave with this, is that in these cases always test one by one, if notice similar errors, in isolated tests, does not need to be in the main script, creates an empty script and tests part by part, isolating things is a good way to understand the problem ;)

-3

An alternative way with format

 # -*- coding: utf-8 -*-
valor_hora = float(input('Valor da hora trabalhada: '))
qtd_horas_trabalhadas = int(input('Quantidade de horas trabalahadas: '))

salario_bruto = valor_hora * qtd_horas_trabalhadas
ir = salario_bruto * 0.11
inss = salario_bruto * 0.08
sindicato = salario_bruto * 0.05
salario_liquido = salario_bruto - (ir + inss + sindicato)

print('''+ Salário Bruto : R$ {0:.2f}
          - IR (11%) : R$ {1:.2f}
          - INSS (8%) : R$ {2:.2f}
          - SINDICATO (5%) : R$ {3:.2f}
          ----------------------------
          = Salário líquido : R$ {4:.2f} '''.format(salario_bruto,ir,inss,sindicato,salario_liquido))

for greater readability:

 # -*- coding: utf-8 -*-
valor_hora = float(input('Valor da hora trabalhada: '))
qtd_horas_trabalhadas = int(input('Quantidade de horas trabalahadas: '))

salario_bruto = valor_hora * qtd_horas_trabalhadas
ir = salario_bruto * 0.11
inss = salario_bruto * 0.08
sindicato = salario_bruto * 0.05
salario_liquido = salario_bruto - (ir + inss + sindicato)

print('''+ Salário Bruto : R$ {salario_bruto:.2f}
          - IR (11%) : R$ {ir:.2f}
          - INSS (8%) : R$ {inss:.2f}
          - SINDICATO (5%) : R$ {sindicato:.2f}
          ----------------------------
          = Salário líquido : R$ {salario_liquido:.2f} '''.format(salario_bruto=salario_bruto,ir=ir,inss=inss,sindicato=sindicato,salario_liquido=salario_liquido))
  • 2

    So you find 0, 1, 2, 3, etc better than the names of the direct variables with f"{}"? What is this better exactly? Performance? Readability?

  • no, but nothing prevents using the names within the format.

  • @Guilhermenascimento of the second form

  • 1

    And this doesn’t seem redundancy to you? Because as I said, the effect of f"{}" compared to the format in the codes presented would be the same, you are only "6" by "half-duzia" and finally you are even letting the more complex readability or if you use in own format the "named" (which would need to pass as objects or params, which would take more time and complexity) and ultimately be much less performatic. There is only one advantage in the format over the f"{}", backward compatibility with Python2, otherwise you just complicated what was simple. Understand that my assessment here is purely technical.

  • The only thing that is written is "An alternate form with format". At no time informs that it is a better or more performative form. Before doing a giant text wanting to look like you know everything you should read the answer correctly.

  • 2

    I don’t mean to sound like I know everything and I never said this, I’m applying a rational and logical question with no personal implications, the only question here is "doesn’t it seem redundancy to you?" Because it’s redundancy, and I did a technical assessment of what was presented, which would only be useful if the code looked for backward compatibility with python2, but rarely python3 codes will run in py2, which is no use having a backward compatible piece. Finally I presented the arguments to you, explained the technical part and you just assumed that I am wanting to "find myself", and my assessment is only technical

  • 2

    The question is "Could someone tell me what the error of this code is". How does this answer the question?

Show 2 more comments

-5

You need to convert to FLOAT

print(f'''+ Salário Bruto : R$ {.format(float(salario_bruto:.2f)}
          - IR (11%) : R$ {ir:2.f}
          - INSS (8%) : R$ {inss:2.f}
          - SINDICATO (5%) : R$ {sindicato:.2f}
          ----------------------------
          = Salário líquido : R${salario_liquido:.2f}''')

I’m also inexperienced but it’s supposed to work

  • 1

    But gross salary is no longer float? The multiplication of an int by a float does not result in a float? type(gross salary) returns float...

Browser other questions tagged

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