Newbie who is a bit stuck!

Hi everyone,

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

Thanks for your help, the code I have created might be super weird to experienced coders but hey, you gotta start somewhere

Yes, you have and you’ve made a good start.

Advice:

  • Don’t use so-called ‘magic numbers’.
    If you have a minimum and a maximum, then use them:

minimum = float(12500)
maximum = float(50270)

Then you can have the likes of if grossWage < minimum

Also, there’s no need to keep using the key word float, once you’ve defined an object, such as grossWage = 25000.00 The decimal point defines the object as a floating point number.

Or…
grossWage = float(input("Wage: "))

I think you need or rather than and in your elif branch (not too sure without testing it).

to add…

I think I see the issue. Try…
elif gross_wages >= minimum+1 and gross_wages <= maximum+1:

… or (without the +1)

elif gross_wages > minimum and gross_wages < maximum:

to add another thought:

If you have taxable_deductions = grossWage *0.2 then taxable_deductions will be a floating point object, by default. You can test the with print(type(taxable_deductions)) which is output <class 'float'>

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 floats 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

There are some gaps in the checks. It might not be a problem in practice, but…
What if gross_wages is, say, 12500.5? None of the conditions will be true.
Also, shouldn’t the gross be split over multiple bands, taxed at 0% for the first 12500, then at 20% for the next 50270, then at 40% for the remainder?

Thank you so much for your feedback, I am still playing with it to try and result in a successful running of the code. I really appreciate the advice and will update if i resolve it!

2 Likes

Thank you for the advice and for the corrections for forum etiquette, I will do this in future posts

2 Likes

Yes, absolutely, however I am trying to keep it simple at this stage, if I manage to resolve it, I will certainly then work to make it a more true-to-life reflection of tax conditions