Calculation of salary needs to limit houses

Asked

Viewed 554 times

1

An employee of a company receives salary increase annually: It is known that:

This employee was hired in 1995, with initial salary of R $ 1.000,00;

In 1996 he received a 1.5% increase over his initial salary;

From 1997 (inclusive), wage increases always correspond to twice the percentage of the previous year.

Make a program that determines the employee’s current salary.

After completing this, change the program allowing the user to enter the employee’s initial salary.

My code:

salario = int(input('Digite o salário do funcionário: '))
ano = 1996
porcentagem = 1.5/100
salario_1 = salario
while ano <= 2020:
    salario = salario + (porcentagem * salario_1)
    ano += 1
    porcentagem += porcentagem
print(salario)

My real doubt is how to limit the print of an integer (Ex.1234567, and only appear --> 123), because the output of my code comes out a little strange sometimes. With the salary being 1000, the output is: 503317465.0 (and I doubt that’s even the right result).

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

4

There are some problems in this code. Besides being too complex, it does not do what the statement asks and produces the correct result. The issue of formatting is just a point that won’t make it correct.

There are several ways to format, I will put one, it is not the most modern, but the whole code is not the most idiomatic even. Just like you can use a more elaborate formula, but I believe that’s not the goal.

Just remembering that for real monetary values can not do this way, since Python has by default numeric type with binary floating type that has no accuracy and they do not lend themselves to if this type of task, would have to use another type or juggle with integer (which wouldn’t even work when asking for simple data entry), but for exercise is ok.

salario = 1000
ano = 1996
while ano <= 2020:
    salario *= 1.015
    ano += 1
print("{0:.2f}".format(salario, 2))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

When the exercise is given so it makes little sense because you don’t need a code to do that, you have all the data and it can be calculated without code, but if you’re going to do the code I would make it as simple as possible, only artificially keeping the values already known to calculate, then would use the compound interest formula.

print(f"{1000 * (1.015 ** (2020 - 1996 + 1)):.2f}")

And continuing what you ask later I could do just this:

print(f"{int(input('Digite o salário do funcionário: ')) * 1.45095:.2f}")

I did not treat possible typing error that would invalidate the entry and generate exception.

  • And I’d have a tip on how to make the code more idiomatic?

  • 1

    That would be another question and change everything, I put the form with f strings to help, and the loop would be done with for, but actually the exercise is bad because it doesn’t need code, so I have to artificially impose a limit as far as I’m going to reduce. If reduce to the limit nothing needs to be written. I did one now using formula, but I think it was not the object, fault the exercise be bad. Then you’d have to start inventing impositions just to make it work.

  • just remembering that if you want exact decimal numbers in Python they are available in the module decimal, then the phrase "because Python has numbers with floating type binary that has no accuracy and does not lend itself to if this type of task" is incorrect. And even if there was no decimal module, traditionally one emulates a fixed point monetary value using an integer, which allows monetary accounts in virtually any language, including C or Python - but integers with no size limit make things much easier in this approach.

  • 1

    So that means Python nay have numbers with binary floating type? It would be the only way my statement is wrong. The only statement I made is that it has. I didn’t say you can’t do it any other way and get the right result.

  • Python uses the standard binary "double' flute point of the C compiler used. The statement you made is the one I put in quotes - and the reading is that the subject of the part that ends in "...and does not lend itself to this kind of task" is the language, not the data type. In the sentence as it is, replace Python with : "because this language has numbers with binary floating type that is not exact and does not lend itself to this type of task" - which language nay would fit? Cobol, maybe.

  • Pinching a part of the text to make its statement be true is not a very fair thing, the text was there whole, to be interpreted as a whole not only with a part.

Show 1 more comment

Browser other questions tagged

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