I am trying to define a conditional function that is the definition of the function depends on the input value. I also want it to run on several different inputs contained in a list.
The output that I am getting is not what I am expecting. For the following inputs: incomes = [500, 1500, 4000] I expect the output to be: 50, 200 but the actual outputs are: -150, 150 and 900 respectively. The outputs that I was expecting are: . I do get the correct output when I enter only one income value to the list.
incomes = [500, 1500, 4000]
for current_income in incomes:
income = current_income
if income <1000:
def tax(income):
return income*0.1
elif 1000<=income<2000:
def tax(income):
return 1000*0.1 +(income-1000)*0.2
else:
def tax(income):
return 1000*0.1+ 1000*0.2 + (income-2000)*0.3
for i in incomes:
result = tax(i)
print(result)
It seems that the order of values in the list matter : I reversed the order of incomes in the list I get the output of: 400, 150, 50. I understand that the problem lies in the interaction of the for loop and if, elsif and else conditions but I do not see what is actually wrong in my code.
Aucun commentaire:
Enregistrer un commentaire