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

2.1 Finding the Max of Three Integers

Any statement may be placed within an if statement, including other if statements. When you want to check multiple conditions there may be a need to put one if statement inside another. It can happen, but not very often. For instance, you may need to know if a value entered by a user is between two numbers. This could be written using two if statements, the outer if statement checking to see if the value entered is greater than some minimum, and the inner if statement checking to see of the value entered is less than some maximum. There are other ways to check to see if a value is between a maximum and minimum, but nested if statements can be used in this kind of circumstance.

Let's consider another possibility. Suppose you are asked to write a program that finds the maximum of three integers. This can be accomplished by writing nested if statements. Figure 2.5 depicts the flow of control for such a program.

We could determine which of the three integers, x, y and z, was the greatest by first comparing two of them, say x and y. Then, depending on the outcome of that condition, we would compare two more integers. By nesting if statements we can arrive at a decision about which is greatest. This code gets a bit complicated because we have three if statements to deal with, two of which are nested inside the third statement.

Fig. 2.5 Max of three integers

Example 2.3 While you wouldn't normally write code like this, it is providedhere to show how if statements may be nested. The code prints the maximumof three integers entered by the user.

1 x = i n t ( i n p u t ("Please enter an integer:"))

2 y = i n t ( i n p u t ("Please enter another integer:"))

3 z = i n t ( i n p u t ("Please enter a third integer:"))

4 i f y > x:

5 i f z > y:

6 p r i n t (z, "is greatest.")

7 e l s e :

8 p r i n t (y, "is greatest.")

9 e l s e :

10 i f z > x:

11 p r i n t (z, "is greatest.")

12 e l s e :

13 p r i n t (x, "is greatest.")

14 p r i n t ("Done.")

2.2 The Guess and Check Pattern

There is no way a good programmer would write a program that included the code that appeared in Example 2.3. It is too complicated. Instead, it would be much better to use a pattern or idiom called Guess and Check. Using this pattern involves first making a guess as to a correct solution and storing that guess in a variable. Then, you use one or more if statements to check that guess to see if it was correct or not. If it was not a correct guess, then the variable can be updated with a new guess. Finally, when the guess has been thoroughly checked, it should equal the value we were looking for.

Example 2.4 Consider the max of three program in Example 2.3. This couldbe rewritten using the guess and check pattern if we first make a guess as tothe maximum value and then fix it if needed.

1 x = i n t ( i n p u t ("Please enter an integer:"))

2 y = i n t ( i n p u t ("Please enter another integer:"))

3 z = i n t ( i n p u t ("Please enter a third integer:"))

4 # Here is our initial guess

5 maxNum = x

6 i f y > maxNum: # Fix our guess if needed

7 maxNum = y

8 i f z > maxNum: # Fix our guess again if needed

9 maxNum = z

10 p r i n t (maxNum ,"is greatest.")

11 p r i n t ("Done.")

The code in Examples 2.3 and 2.4 get the same input and print exactly the same thing. However, the code in Example 2.4 is much easier to understand, mainly because the control flow is simplified by not having nested if statements. Notice that no else clauses were needed in Example 2.4. So, the code is simplified by having two if statements instead of three. It is simplified by having no nested if statements. Finally it is simplified because there are no use of else clauses in either of the if statements.

Practice 2.3 Use the guess and check pattern to determine if a triangle is aperfect triangle. A perfect triangle has side lengths that are multiples of 3, 4and 5. Ask the user to enter the shortest, middle, and longest sides of a triangleand then print “It is a perfect triangle “if it is and “It is not a perfect triangle”if it isn't. You may assume that the side lengths are integers. Let your guess bethat the message you will print is “It is a perfect triangle”.

 
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