Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

2.3 Choosing from a List of Alternatives

Sometimes you may write some code where you need to choose from a list of alternatives. For instance, consider a menu driven program. You may want to print a list of choices and have a user pick from that list. In such a situation you may want to use an if statement and then nest an if statement inside of the else clause. An example will help clarify the situation.

Example 2.5 Consider writing a program where we want the user to enter twofloats and then choose one of several options.

1 x = f l o a t ( i n p u t ("Please enter a number:"))

2 y = f l o a t ( i n p u t ("Please enter a second number:"))

3

4 p r i n t ("1) Add the two numbers")

5 p r i n t ("2) Subtract the two numbers")

6 p r i n t ("3) Multiply the two numbers")

7 p r i n t ("4) Divide the two numbers")

8

9 choice = i n t ( i n p u t ("Please enter your choice:"))

10

11 p r i n t ("The answer is:",end="")

12

13 i f choice == 1:

14 p r i n t(x + y)

15 e l s e :

16 i f choice == 2:

17 p r i n t (x - y)

18 e l s e :

19 i f choice == 3:

20 p r i n t (x * y)

21 e l s e :

22 i f choice == 4:

23 p r i n t(x / y)

24 e l s e :

25 p r i n t ("You did not enter a valid choice.")

Do you notice the stair step pattern that appears in the code in Example 2.5? This stair stepping is generally considered ugly and a nuisance by programmers. Depending on how much you indent each line, the code can quickly go off the right side of the screen or page. The need to select between several choices presents itself often enough that Python has a special form of the if statement to handle this. It is the if-elif statement. In this statement, one, and only one, alternative is chosen. The first alternative whose condition evaluates to True is the code that will be executed. All other alternatives are ignored. The general form of the if-elif statement is given here.

< statements before if statement > if < first condition >:

< first alternative >

elif < second condition >:

< second alternative >

elif < third condition >:

< third alternative > else :

< catch all alternative >

< statements after the if statement >

There can be as many alternatives as are needed. In addition, the else clause is optional so may or may not appear in the statement. If we revise our example using this form of the if statement it looks a lot better. Not only does it look better, it is easier to read and it is still clear which choices are being considered. In either case, if the conditions are not mutually exclusive then priority is given to the first condition that evaluates to true. This means that while a condition may be true, its statements may not be executed if it is not the first true condition in the if statement.

Example 2.6 Here is a revision of Example 2.5 that looks a lot nicer.

1 x = f l o a t ( i n p u t ("Please enter a number:"))

2 y = f l o a t ( i n p u t ("Please enter a second number:"))

3

4 p r i n t ("1) Add the two numbers")

5 p r i n t ("2) Subtract the two numbers")

6 p r i n t ("3) Multiply the two numbers")

7 p r i n t ("4) Divide the two numbers")

8

9 choice = i n t ( i n p u t ("Please enter your choice:"))

10

11 p r i n t ("The answer is:",end="")

12

13 i f choice == 1:

14 p r i n t(x + y)

15 e l i f choice == 2:

16 p r i n t (x - y)

17 e l i f choice == 3:

18 p r i n t(x * y)

19 e l i f choice == 4:

20 p r i n t(x / y)

21 e l s e :

22 p r i n t ("You did not enter a valid choice.")

Practice 2.4 Write a short program that asks the user to enter a month and prints a message depending on the month entered according to the messages in Fig. 2.6. Then use the step into and over ability of the debugger to examine the code to see what happens.

Month

Message

January

Hello Snow!

February

More Snow!

March

No More Snow!

April

Almost Golf Time

May

Time to Golf

June

School's Out

July

Happy Fourth

August

Still Golfing

September

Welcome Back!

October

Fall Colors

November

Turkey Day

December

Merry Christmas!

Fig. 2.6 Messages

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel