I just started learning to use python. I decided to try and create an
income tax calculator for funs.
I am not sure where I am going wrong with the final part of my code (section 20% tax bracket). It appears to be where I am attempting to create a condition between 12,500 and 50,270
elif float(gross_wages) >= float(12501) and <= float(50271):
taxable_deductions = float(gross_wages) * .2
Please enclose your code in triple backticks to preserve the formatting:
```
your code
goes here
```
And avoid screenshots - email users can’t see them and the visually
impaired can have trouble with them and anyway we cannot copy/paste from
them. Just paste the code inline between triple backticks.
Looking at your condition:
elif float(gross_wages) >= float(12501) and <= float(50271):
You actually want:
elif float(gross_wages) >= float(12501) and float(gross_wages) <= float(50271):
But also, Python has nice cascading conditions which mean you can write
it like this:
elif float(12501) <= float(gross_wages) <= float(50271):
BTW, you probably want <=
on one side and just <
on then other, as
ranges like this tend to be inclusive at one end and exclusive at the
other i.e. the next tax bracket up starts at 50271
but you code
implies that it starts at 50271.01
(assuming dollars and cents).
Also, I suspect you’re working with strings, maybe from a CSV file. It
is usually easier to convert them to float
s as you read them. Example:
# as you read the values
gross_wages = float(gross_wages)
# in your tests:
elif 12501 <= gross_wages <= 50271:
Also, 12501
isn’t a string, it is an integer. You don’t need to
convert it to float
explicitly, Python will do that when you do
arithmetic with a float, or a comparison as you’re doing here.
Python won’t implicitly convert a string to float
, they’re
inherently very different things. Whereas an integer and a floating
point number are inherently numeric values.
Cheers,
Cameron Simpson cs@cskk.id.au