[Tutor] Tax Brand

Steven D'Aprano steve at pearwood.info
Wed Oct 9 01:39:36 CEST 2013


On Tue, Oct 08, 2013 at 10:38:02PM +0200, Thabile Rampa wrote:

> taxBAND = None

You set taxBAND to None here. This is the only time you assign a value 
to taxBAND.


> if (GrossSalary < 50000):
>     taxdeducted= 0.05*(GrossSalary -costs)
>     netSalary = GrossSalary - taxdeducted
>     while GrossSalary < 50000:
>          print taxBAND == 1

This will be an infinite loop. You test whether GrossSalary is less than 
50000, and if it is, you print True or False (it will always be False, 
because taxBAND at this point is still None). Then you test whether 
GrossSalary is less than 50000, which of course it will be, since you 
haven't changed it, then print False again. Then you test whether 
GrossSalary is still less than 50000, which is will be, then print False 
again. And again. And again. And again. Until the sun burns out and the 
planet is destroyed. (Or you type Ctrl-C to halt the program.)

What was your intention in this bit of code? Can you explain in English 
what you hoped to do? The first step in programming is to understand in 
your own head what you hope to do, then you do it. If you're unclear 
what needs to be done, how can you do it?


> elif (GrossSalary >= 50000 and GrossSalary < 150000):
>      taxdeducted = 0.1*(GrossSalary - costs)
>      netSalary = GrossSalary - taxdeducted

You can write that test more simply as:

elif 50000 <= GrossSalary < 150000:



> print line
> print "\t\t BURS STATEMENT: ",name
> print line
> print "Gross", "\t Costs", "\t Tax BAND", "\tTax Deducted", "\t Net Salary"

You can simplify this by making it a single string:

print "Gross\tCosts\tTax BAND\tTax Deducted\tNet Salary"


> print
> GrossSalary,"\t",costs,"\t\b",taxBAND,"\t",taxdeducted,"\t\t",netSalary
> print line

What is the purpose of the \b above? Is that a typo? Why do you 
sometimes have one tab \t and sometimes two \t\t?

In this case, since you have never changed the value of TaxBAND, it is 
still set to the same value you gave it all the way back at the 
beginning of the program. Remember this line?

taxBAND = None

taxBAND will still be None, since you have never set it to something 
else. In English, you might say:

If the gross salary is less than 50000, then set taxBAND to 1; 
otherwise, if the gross salary is less than 150000, then set taxBAND to 
2; otherwise ...

In your code, you already have tests for the gross salary:

if GrossSalary < 50000:
    # things to do when gross salary is less than 50000


One of those "things to do", which you don't do, is to set taxBAND to 1, 
or another way to say the same thing, assign 1 to taxBAND. Do you 
remember how to assign values to a variable?

taxBAND = None  # assign None to the variable taxBAND


> I haven't put in the whole code because it's too long. I instead want to
> focus on the problem which can be found in the if section: print taxBAND ==
> 1
> 
> I have tried all the different operators, from augmentation to logical to
> comparison, but I still get errrors. The errors I have acquired so far are:

Ah, the good ol' "Make Random Changes In The Hope That The Error Will Go 
Away" strategy! :-)

Making random changes to the code will just give you random errors. You 
must understand what you are writing. The first step is to understand 
the errors:

> taxBAND += 1
> TypeError: unsupported operand type (s) for +=: 'NoneType' and 'int'

Remember, taxBAND is still set to None. Here you are trying to add 1 (an 
int, or integer) to None. Python does not allow that.

Why are you trying to *add* 1? Why not subtract 1, or add 15? Ask 
yourself, what am I actually trying to accomplish?


> Traceback (most recent call last):
> File "X:/xxx/Xxxx.py", line 30, in <module>
> taxBAND = I
> NameError: Name 'I' is not defined

In this case, you are telling Python "assign the value of the variable I 
to the variable taxBAND", and Python says "I? What the hell is variable 
I? Never heard of it." Which is absolutely correct, since nowhere do you 
assign a value to I:

# you don't do anything like this
I = 1972


> Traceback (most recent call last):
> File "X:/xxx/Xxxx.py", line 31, in <module>
> taxBAND += 1
> TypeError: unsupported operand type (s) for +=: 'NoneType' and 'int'

This is exactly the same error as above.


> Traceback (most recent call last):
> File "X:/xxx/Xxxx.py", line 31, in <module>
> taxBAND = I
> NameError: Name 'I' is not defined

And so is this.



-- 
Steven


More information about the Tutor mailing list