PYTHON MADE SIMPLE (2015)

Chapter 5: Use of if – elif statements in making Multiple Tests in Python

When one would like to create more than two distinct cases and the conditions have only two possible conditions which can either be true or false. The only direct choice may be between the other two options. For instance, when one has about 20 questions, you can be able to make more cases by use of more questions. When there are more than two choices, a single test can be used to reduce the possibilities and other tests added will be able to lower the possible further. Statements will be required to be placed in a indented statement block and another added choice will be an extra if statement to the block.

For instance, a teacher would like to make a conversion table for numerical marks to grades (A, B, C, D, E or F). The marks limits are 90, 80, 70, 60 respectively. Hence, this can be resolved very easily by stating all the possible options in the else clause:

It is not mandatory that the indentation be used for every case when using the if  -- else expression.  It is indeed unique and must be strictly followed in this case.

However, the indentation can be cleared using an alternative if … elif clause. The elif combines both the if and else clause to become an elif block in the python code:

See this code below and note the indentation applied in this case.

The syntax applied in this use of if, elif –else clause is illustrated below. This helps you

Note that all use of if, elif, and the final else are all aligned on one line. The number of elif to be used is not limited with every of the line followed by an indented block.

What happens is that one of the indented block will be the one to be executed, when it is the one that corresponds to the first true conditions set using the if statement. Otherwise, when all the condition are false, the final else will be executed.

The use of if and elif is an important forms of python Contraction. Care need be observed when using the contracted form of python codes. It is supposed to written as elif and not elseif.  The gradingmarks.py is a good example of the use of this code illustrated above.

Alternative syntax

The other way in which the if statement can be applied is by the use of the  if-elif-and removing the else statement in the code. This suggests that the syntax if- elif- else is modified so that the final block of code with else is removed or omitted. This is the same as the normal if statement that does not include an else in its code. This allows for the no block of code to be executed and only occurs when none of the conditions in the tests is true. However, when an else code is included, one of the indented code has to be executed. In the absence of the else code; at most one of the code as to be executed.

This code will only be able to print a line whenever there will be a problem with the suitcase.

Here is an example of the code:

PRACTICE EXERCISE

Sign Exercise

Develop a program and save it as identity.py to be used to collect numbers from clients. Print out the number and categorize it as positive, negative or zero

Grade Exercise

Write the program gradingmarks.py and save it now as gradingmarks2.py.

Make modification of the python code so as to have a letter grade function that makes the opposite of the grades starting from F, E,D, … A.

Try to run your code and make different inputs to ensure that your code is working and providing you with the right output in Idle.

Wages Exercise

Similarly, make modification of the wages.py and save it as wages1.py. However, ensure that you create a problem that indicates that people are paid double the money for any hours worked above 50 hours per week. This will show that they get to be paid normal rate within normal hours for 40 hours a week, and $1.5 for hours worked extra of 40 but less 50 hours and double any hours worked above 50 hours.

This can be expressed as below for a total of:

10*40 + 1.5*10*20 + 2*10*5 = $800.

Run your program several time and make modifications to ensure that it provides you with the right calculations. Use comments where appropriate to help people understand your code.