Home > OS >  How to calculate tax in python?
How to calculate tax in python?

Time:11-17

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

CodePudding user response:

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.

  • Related