Percentage with if in Python

Asked

Viewed 454 times

-4

Hello, I’m a beginner in Python and I came across a great question in a lesson I’m doing. I’m doing a program that takes user information such as: name, date of birth, position and salary. After this it sets an expense limit according to the user’s salary and age.

My next exercise is to ask the user to enter the value of a product, and if the value is below 60% of the limit, send an msg, between 60% and 90% other msg and between 90% and 100%, another msg.

My question is, how to calculate these percentages within an if in python?

I’m not going to quote my code here because maybe I’d get the answer on a silver platter... so I’d like a little help on this percentage if question. Thank you!!!

  • "I’m not going to quote my code here because I might get the answer on a silver platter" - It’s actually the opposite: when you don’t put any code, gives the impression that you have done nothing and want us to do everything for you (even if it is not that, it is the impression that passes). The ideal is to put what you’ve tried and explain why it didn’t work - which is what we call [mcve]

  • ah... beauty did not know. It’s for a college exercise and I didn’t want to have an answer with all the calculations already done and so on... but I’ve done all the rest of the code yes, just missed this calculation. Thank you for clarifying, I will improve in the next!

2 answers

1

Theoretically to calculate the percentage of a given valor, just divide it by 100 and multiply it by numero that you want, or simply multiply the valor for taxa, where the rate will always be 0,x.

for example, I want to know 50% of 2000 how much of: (2000 / 100) * 50 or 2000 * 0,50

75% of 325 (325/100) * 75 or 325 * 0,75

LIMITE=1000
PRODUTO=901

if PRODUTO < LIMITE * 0.60 :
    print('valor menor que 60%')
elif PRODUTO >= LIMITE * 0.60 and PRODUTO <= LIMITE * 0.90:
    print('entre 60% e 90')
else:
    print('maior que 90%')
    
abs!
  • Thank you very much Marcus!! It helped a lot, it was simpler than I imagined... rs

0


Only to complement the another answer:

In the elif no need to test whether PRODUTO >= LIMITE * 0.6.
If you didn’t get into the if PRODUTO < LIMITE * 0.60, is because the product is not less than LIMITE * 0.60. So we arrived at the elif is because it is certainly greater or equal. Then just test the other condition:

limite = 1000
produto = 901

if produto < limite * 0.6:
    print('valor menor que 60%')
elif produto <= limite * 0.9:
    print('entre 60% e 90')
elif produto <= limite:
    print('entre 90% e 100%')

That is, if it is less than 60%, it enters the if.

If you didn’t get into the if, is because it is greater than or equal to 60%. Then elif just test if it is less than or equal to 90%. Test again if it is >= limite * 0.6 is redundant and unnecessary.

I also included a test to see if it’s between 90% and 100%, which seems to be a requirement. It has not been defined what to do if the value is greater than 100% of the limit, or if it is negative, but if it were, it would be enough to add new conditions. Ex:

if produto < 0:
    print('valor não pode ser negativo')
elif produto < limite * 0.6:
    print('valor menor que 60%')
elif produto <= limite * 0.9:
    print('entre 60% e 90')
elif produto <= limite:
    print('entre 90% e 100%')
else:
    print('valor maior que o limite')

The idea is the same: if it is less than zero it enters the if. If you don’t go in there, it’s because it’s definitely greater than or equal to zero, then in the first elif I only test if it’s less than 60%. And so on...

Also note that I wrote the numbers as 0.6 and 0.9. How they are numerical literals, the extra zeroes (0.60 and 0.90) make no difference.

And I used the variable names with lower case letters, following the PEP 8 conventions.

Browser other questions tagged

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