| Calculating a different Tax Regime | A Programme with Decision Capabilities | Practice | Exercise | Assignment Part 2 |
On completion of this section you shall know:
In all aspects of life we make decisions. Examples are if the buses are
running we shall go into town, otherwise we shall study. In programming we have
to make decisions as well or to be more precise we shall tell the computer how
to make decisions. To demonstrate this we shall modify the programme created in
the previous chapter so tax is calculated in a different way. In the previous
example we had a simple flat tax rate of 25%. In real life tax rates are never
as simple as this. If the salary is low then the tax rate will also be low but
a higher salary will incur higher tax rate. In our case we shall modify the
simple payroll programme we met in Chapter Error! Reference source not found. so that it will calculate tax in two different
ways depending on the value of the gross pay.
Calculating different Tax Regimes
In
the chapter ‘A simple Python Programme’ tax
was calculated as a flat 25% of the gross. As stated above tax calculation is
not quite this simple. Most governments have different levels of taxation
depending on a person’s income level. In our case we shall assume that any
gross which is $500 or less will be taxed at 25% whereas any gross that exceeds
$500 will be tax at 25% of the first $500 and 33% of the remainder. Let us look
below at our modified code to see how this is done.
Listing 1
|
1 |
#C3Payroll Programme
Decisions.py |
|
2 |
strName=input("Enter employee's full
name: ") |
|
3 |
floatHours=float(input("Enter value for
hours: ")) |
|
4 |
floatRate=float(input("Enter value for
rate: ")) |
|
5 |
floatGross=floatHours * floatRate |
|
6 |
if floatGross<500: |
|
7 |
floatTax=floatGross * 0.25 |
|
8 |
else: |
|
9 |
floatTax=125+(floatGross - 500) * 0.33 |
|
10 |
floatNet=floatGross-floatTax |
|
11 |
print("Gross is
" + str(floatGross)) |
|
12 |
print("Tax is
" + str(floatTax)) |
|
13 |
print("Net is
" + str(floatNet)) |
Apart from lines 6 – 9 the programme shown in Listing 1 is identical to the programme we met in the chapter ‘A simple Python Programme’ and therefore we shall concentrate solely on this group of lines.
As stated earlier we have a programme that can make a decision between calculating the tax in one of two different ways. The main decision point is at line 6. Here we have the keyword if followed by the condition floatGross<500.
A condition, of course, can be either true or false. If the variable floatGross has a value of 400 then the condition is true, whereas if it has a value of 600 then the condition is false.
How does Python work with these conditions? At line 6 if floatGross has a value of 400 then the condition is true. In this case programme control passes to the body of the if, or in other words to line 7. Here the tax is calculated as simply 25% of the gross. Once this is finished programme control passes from the body of the if to the command following the body of the else, or in other words to line 10. Therefore if floatGross had a value of 400 then floatTax would have a value of 400 * 0.25, on in other words 100.
On the other hand if floatGross has a value of 800, then at line 6 the condition will be false since 800 is not less than 500. If this is the case programme control jumps from line 6 directly to line 9, thus skipping line 7. Again with floatGross having a value of 800 then at line 9 floatTax would be given a value of 125 +(800-500) * 0.33 = 125 + 300 * 0.33 = 155 = 125 + 99 = 224.
Figure 1 below shows two runnings of the programme, the first where floatGross has a value of 400 and the second where the same variable has a value of 800

Figure 1
Copy the code in Error! Reference source not found.into the programme into a text processor and save it.
Next run it a number of times. Use
different combinations of hours and rate so that some give a gross less than
500 and others give a gross greater than 500.
Before running the programme each time first check using pen and paper
what the values for the gross, tax and net should be, then check that the
programme outputs against the same results.
Regarding lines 6 – 9 in Listing 1 above answer the following questions:
1.
If floatGross has a value of 450 what line or lines would be
executed?
2.
Of floatGross has a value of 560 what lines would be executed?
3.
If floatGross has a value of 700, what would the value of floatTax be?
4.
If floatGross has a value of 200, what would the value of floatTax be?
Modify the application you have created for Assignment Part 1 so that it
can process customer discounts. If the
total is more than $100 then the customer receives a 5% discount otherwise no
discount is given. The discount must be
subtracted from the subtotal before the GST is calculated.
You must add an extra variable to
your programme in order to hold the value of the discount. The value of the discount must be printed
along with the other values.