Zetto - 2 months ago 6

Python Question

I'm pretty new to Python and I'm going through a starter book. The code isn't written in English so I tried my best to translate, hope you guys understand.

It has this exercise where we calculate the taxes from the user salary:

`salary = float(input("Enter your salary to taxes calculation: "))`

base = salary

taxes = 0

if base > 3000:

taxes = taxes + ((base - 3000) * 0.35)

base = 3000

if base > 1000:

taxes = taxes + ((base - 1000) * 0.20)

My problem is when the input is bigger than 3000, for example, if I run the code with the salary of 5000, the result will be 1100. But when I do the 'same' math on the calculator the result is 700, so I'm lost in here, could someone explain it please?

Answer

Alright, let's walk through it with your example of 5000

```
salary = float(input("Enter your salary to taxes calculation: "))
base = salary
# base = 5000
taxes = 0
if base > 3000: # base is larger than 3000, so we enter the if statement
taxes = taxes + ((base - 3000) * 0.35)
# taxes = 0 + ((5000 - 3000) * 0.35)
# taxes = 0 + 700
# taxes = 700
base = 3000 # base is set to 3000
if base > 1000: # base was set to 3000 in the line above, so we enter the if statement
taxes = taxes + ((base - 1000) * 0.20)
# taxes = 700 + ((3000 - 1000) * 0.20), remember taxes is already 700 from above
# taxes = 700 + 400
# taxes = 1100
```

since it is two `if`

statements and not an `if`

and an `else`

we evaluate both statements when `base`

is set larger than 3000. I hope that helps.

Source (Stackoverflow)

Comments