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

2.5 Short Circuit Logic

Once in a while using the guess and check pattern may not produce the desired results. There are situations where you may want to evaluate one condition only if another condition is true or false. An example should make this clear.

Example 2.10 Consider a program that checks to see if one integer evenly divides another.

1 top = i n t ( i n p u t ("Please enter the numerator:"))

2 bottom = i n t ( i n p u t ("Please enter the denominator:"))


4 i f bottom != 0 and top % bottom == 0:

5 p r i n t ("The numerator is evenly divided by the denominator.")

6 e l s e :

7 p r i n t ("The fraction is not a whole number.")

Dividing top by bottom would result in a run-time error if bottom were 0. However, division by 0 will never happen in this code because Python, and most programming languages, uses short-circuit logic. This means that since both A and B must be true in the expression A and B for the expression to evaluate to true, if it turns out that A evaluates to false then there is no point in evaluating B and therefore it is skipped. In other words, Boolean expressions are evaluated from left to right until the truth or falsity of the expression can be determined and the condition evaluation terminates. This is exactly what we want in the code in Example 2.10.

Practice 2.6 In Minnesota you can fish if you are 15 years old or less and yourparent has a license. If you are 16 years old or more you need to have yourown license. Write a program that uses short circuit logic to tell someone ifthey are legal to fish in Minnesota. First ask them how old they are, whetherthey have a license or not, and whether their parent has a license or not.

2.6 Comparing Floats for Equality

In Python, real numbers or floats are represented using eight bytes. That means that 264 different real numbers can be represented. This is a lot of real numbers, but not enough. Since there are infinitely many real numbers between any two real numbers, computers will never be able to represent all of them.

Because floats are only approximations of real numbers, there is some round-off error expected when dealing with real numbers in a program. Generally this round-off error is small and is not much of a problem unless you are comparing two real numbers for equality. If you need to do this then you need to subtract the two numbers and see if the difference is insignificant since the two numbers may be slightly different.

So, to compare two floats for equality you can subtract the two and see if the difference is small relative to the two numbers.

Example 2.11 This program compares a guess with the result of dividing two floats and tells you if you are correct or not.

1 top = f l o a t ( i n p u t ("Please enter the numerator:"))

2 bottom = f l o a t ( i n p u t ("Please enter the denominator:"))


4 guess = f l o a t ( i n p u t ("Please enter your guess:"))


6 result = top/bottom

7 biggest = a b s (result)


9 i f a b s (guess) > biggest:

10 biggest = a b s (guess)


12 # require the answer is within 1/10 th Percent

13 # of the correct value.

14 i f a b s ((guess -result )/ biggest) < .001:

15 p r i n t ("You guessed right!")

16 e l s e :

17 p r i n t ("Sorry , that 's wrong. The correct value was",result)

Notice in the program in Example 2.11 that the abs function returns the absolute value of the float given to it so it doesn't matter if the numbers you are comparing are positive or negative. The code will work either way. In this example, 0.001 or 1/10th of 1 % difference was deemed close enough. Depending on your application, that value may be different.

Practice 2.7 Use the guess and check pattern to determine if a triangle is aperfect triangle. You must allow the user to enter any side length for the threesides of the triangle, not just integers. A perfect triangle has side lengths thatare multiples of 3, 4 and 5. Ask the user to enter the three side lengths and thenprint “It is a perfect triangle” if it is and “It is not a perfect triangle” if it isn't.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science