| An if..elif..else construct | If..elif..else - another version | Summary |
| Practice | Exercise | Assignment Part 3 |
On completion of this chapter you will know how to make the programme
perform more complex decisions using the if..elif..else
construct
The if..elif..else construct
The if..else construct, which we have encountered in
the previous chapte is fine when we have only to
choose between two alternatives. On the other hand if we have to choose between
a larger number of alternatives, the if..else construct, even though it
can handle the situation, can be very clumsy. In this situation we use the if..elif..elif construct.
In order to explore this construct we shall extend our payroll
application further so that it can handle superannuation calculation.
Superannuation calculation normally involves taking a percentage of an employee’s
pay and putting it into his superannuation account. The method we shall employ
to calculate the contributions is that each employee is given a super code
number and this number determines the percentage of his gross that will be
deducted and put into his super account. In our case the percentage taken will
be according to the following table:
|
Super code |
Percentage |
|
0 |
0% |
|
1 |
5% |
|
2 |
10% |
|
3 |
15% |
|
4 |
20% |
What this means is that if the superannuation code is 0 then the
employee pays no superannuation, whereas if the superannuation code is 1 then
the employee pays 5% of his gross pay towards the superannuation. The same
applies for the other possible values of the superannuation code.
On the other hand, if the value for superannuation is any number other
than those in the range 0 – 4, then calculation proceeds and a message is
printed indicating that a faulty superannuation code has been entered, but no
contributions are deducted from the employee’s pay.
Below in Listing 1 we have the next upgrade on our payroll programme,
where this version is able to work out superannuation contributions based on a
superannuation code entered by the user. As always many of the lines in the
programme correspond with its previous version, and again we will only
concentrate on the new lines that we have added. These are line 5 and lines 11 - 24
Listing 1
|
1 |
#C4Payroll Complex 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 |
intSuperCode=int(input("Enter value for
Superannuation code: ")) |
|
6 |
floatGross=floatHours * floatRate |
|
7 |
if floatGross<500: |
|
8 |
floatTax=floatGross * 0.25 |
|
9 |
else: |
|
10 |
floatTax=125+(floatGross - 500) * 0.33 |
|
11 |
if intSuperCode == 0: |
|
12 |
floatSuperAmt
= 0.0 |
|
13 |
elif intSuperCode == 1: |
|
14 |
floatSuperAmt
= floatGross * 0.05 |
|
15 |
elif intSuperCode == 2: |
|
16 |
floatSuperAmt
= floatGross * 0.1 |
|
17 |
elif intSuperCode == 3: |
|
18 |
floatSuperAmt
= floatGross * 0.15 |
|
19 |
elif intSuperCode == 4: |
|
20 |
floatSuperAmt
= floatGross * 0.2 |
|
21 |
else: |
|
22 |
floatSuperAmt
= 0.0 |
|
23 |
print("Faulty value for
super code") |
|
24 |
print("Superannuation
is calculated as zero") |
|
25 |
floatNet=floatGross-floatTax-floatSuperAmt |
|
26 |
print("Gross is " + str(floatGross)) |
|
27 |
print("Tax is " + str(floatTax)) |
|
28 |
print("Superannuation contribution is: " + str(floatSuperAmt)) |
|
29 |
print("Net is " + str(floatNet)) |
Line 5 is similar to the two lines above it except that instead of using
the function float() it uses the
function int() in order to convert the user’s input
into an integer value, before storing that value in the variable intSuperCode.
Also notice that the name of the variable starts with ‘int’
as a reminder to the user that the value contained in it is an integer.
Lines 11 – 24 contain the if..elif..(else). This is a construct used when we need to
check a variable for a number of values and perform a different action
depending on the value. In our case here
we need to check the variable intSuperCode for values in the range 0 – 4 inclusive.
At line 11 it starts off with a normal if construct where intSuperCode is
checked for a value of zero. If it does
have a value zero then line 12 is executed and zero is
stored in floatSuperAmt.
Once this line is executed then programme control jumps out of the if..elif..else construct and execution resumes at line 25.
If, on the other hand, intSuperCode has a value other than zero then line 12
is skipped and at line 13 it is checked for having a value of 1. Similarly at lines 15, 17 and 19 it is
checked for the values 2, 3 and 4. Supposing it has a value of 2 then it is
tested for a value of 1 at line 13. Since this will return a value of false
line 14 is ignored and the variable is tested for a value of 2 at line 15. This will return a value of true and
therefore line 16 is executed, where floatSuperAmt is given a value which is 10% of floatGross. Once this line is executed programme control
jumps out of the if..elif..else construct
and execution resumes at line 25.
Finally supposing that the value of intSuperCode is12 or -5 or any
other value that is not in the range 0 – 4, what happens with our
construct? What happens is the value of intSuperCode is
tested at lines 11, 13, 15, 17 and 19 and false is returned in all cases. Finally execution arrives at line 21 where it
meets the else keyword. The fact that programme execution has reached
the else means that intSuperCode has
a value other than numbers in the range 0 – 4 and the code beyond beyond else,
i.e. lines 22, 23 and 24, will be executed in order to deal with this
situation. In our case what we wish to
do is firstly inform the user that an out-of-range code was entered for the superannuation
code and to set the superannuation contributions to zero.
Figure 1 below shows the different outputs that the programme produces
when on successive runnings the values 0, 2, 3, 4 and
12 were entered the superannuation code.
Figure 1
In the example above, for each different value of intSuperCode a different action
was performed. But what if superannuation was calculated at
5% if the value of the code was 1, 2 or 3. The table below is an example
of this situation.
|
Super code |
Percentage |
|
0 |
0% |
|
1 |
5% |
|
2 |
5% |
|
3 |
5% |
|
4 |
7% |
|
5 |
10% |
|
6 |
10% |
|
7 |
20% |
|
8 |
20% |
|
9 |
20% |
In order to solve this issue we can use the code
in Listing 1 with only the slightest modification. The first of those modifications is shown in
line 13 below where the first elif tests whether intSuperCode has a value of either 1,
2 or 3. Line 17 tests whether the same
variable has a value of either 5 or 6,
while line 19 tests if it has a value of 7, 8 or 9.As before the else at line 21 mops up any problems
with the value of the superannuation code being out of range,
|
1 |
#C4Payroll Complex Decisions1.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 |
intSuperCode=int(input("Enter
value for Superannuation code:
")) |
|
6 |
floatGross=floatHours
* floatRate |
|
7 |
if floatGross<500: |
|
8 |
floatTax=floatGross * 0.25 |
|
9 |
else: |
|
10 |
floatTax=125+(floatGross - 500) * 0.33 |
|
11 |
if intSuperCode==0: |
|
12 |
floatSuperAmt=0 |
|
13 |
elif (intSuperCode==1 or intSuperCode==2
or intSuperCode==3): |
|
14 |
floatSuperAmt=floatGross*0.05 |
|
15 |
elif intSuperCode==4: |
|
16 |
floatSuperAmt=floatGross*0.07 |
|
17 |
elif intSuperCode==5 or intSuperCode==6: |
|
18 |
floatSuperAmt=floatGross*0.1 |
|
19 |
elif (intSuperCode==7 or intSuperCode==8
or intSuperCode==9): |
|
20 |
floatSuperAmt=floatGross * 0.2 |
|
21 |
else: |
|
22 |
floatSuperAmt=0 |
|
23 |
print("Faulty value for
super code") |
|
24 |
print("Superannuation
is calculated as zero") |
|
25 |
floatNet=floatGross-floatTax-floatSuperAmt |
|
26 |
print("Gross is " + str(floatGross)) |
|
27 |
print("Tax is " + str(floatTax)) |
|
28 |
print("Superannuation contribution is: " + str(floatSuperAmt)) |
|
29 |
print("Net is " + str(floatNet)) |
Listing 2
Figure2
Figure 2 above shows the different outputs produced when intSuperCode is given the values 4, 5, 6, 8 and 9.
Summary
If we wish to make a choice between two different pathways, depending on
the value of a condition then we use the if..else
construct. The body of the if is
executed if the condition is true, otherwise the body
of the else is executed. The bodies
of both can be as small as one line of code or as large as we wish.
If we wish to test a variable and take a number of different paths
depending on its value then we use the if..elif..else
statement. The if..elif part of
it tests the variable for values within the accepted range while the else part deals with values outside the
range
Copy the codes in Listing 1 and Listing 2 into two separate files and
save them as .py files.
Run both of them using the sample inputs
shown in Figure 1 and Figure 2. Then
run them with your own data. Make sure that for the superannuation code you
will test with values that are within the range and also with values outside of
the range.
What decision construct should you use:
1.
if you wish
to perform one type of processing if a variable has a value is less than 200
and another type of processing if the value 200 or more?
2.
If a variable can have any value in the range 4, 5,
7, 8, 9 and you wish to do a different processing for each value.
Modify the code you created for Assignment Part 2 so that it can
accommodate a if..elif..else construct. The method of calculating the discount is to
be altered. Now the user will have to
enter a discount code into the system.
He will be prompted for this in the same way as for the amount sold and
the unit price. Discount will be
calculated according to the following table.
|
Discount Code |
Percentage |
|
1 |
0 |
|
2 |
3 |
|
3 |
7 |
|
4 |
10 |
The calculation of the total will remain the same as in Assignment Part 2.