Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate tax in python?

I need to write a function compute_tax(money_list) that calculates the total tax for a given list of financial amounts. The rich (200 money and more) pay a tax of 20. Those who are not rich, but have at least 100 money, pay a tax of 10. The others do not pay the tax. I have prepared the basis of the function, which needs to be fixed and finished.

def compute_tax(money_list):
    tax = 0
    for money in money_list:
        if money >= 200:
            tax += 20
        elif money >= 100:
            tax += 10
        else:
            tax += 0
        money += tax
    return tax

print(compute_tax([50, 20, 80]))
print(compute_tax([50, 120, 80, 480]))
print(compute_tax([250, 120, 170, 480, 30, 1000]))
print(compute_tax([250, 120, 70, 4080, 30, 120, 600, 78]))

Needed output have to be:

0
30
80
80

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have two issues in your code. Firstly you just check for money == 100 in your first if Statement and secondly you assign tax = 0 in your else statement. To correct:

def compute_tax(money_list):
    tax = 0
    for money in money_list:
        if money >= 100 and money < 200:
            tax += 10
        elif money >= 200:
            tax += 20
        else:
            tax += 0
        money -= tax
    return tax


print(compute_tax([50, 120, 80, 480]))
print(compute_tax([250, 120, 170, 480, 30, 1000]))
print(compute_tax([50, 20, 80]))

Simplier u can just check for money <100, money >= 200 and else as matszwecja
pointed out.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading