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

3.4 The Guess and Check Pattern for Lists

While the in operator works well to test for membership in a sequence, it won't work in all situations. Sometimes we need to know if a value with some property other than equality is in a sequence. In these circumstances, the guess and check pattern may be appropriate. The guess and check pattern that we learned about in the last chapter can be applied to sequences, too. You still make a guess at the beginning of the pattern, but then you fix your guess while executing a loop over each element in the sequence you are working with. An example will make things clear.

Example 3.8 Assume we want to know if the user enters an even number in a list of numbers. Here is some code that will decide if one of those numbers is even.

1 s = i n p u t ("Please enter a list of integers:")

2 lst = s.split () # Now lst is a list of strings.

3

4 # make a guess first

5 containsEven = False

6

7 # the iterate over the list

8 f o r element i n lst:

9 x = i n t (element)

10 # check your guess in the loop

11 # and fix it if needed

12 i f x % 2 == 0:

13 containsEven = True

14

15 # after the loop you know whether

16 # your guess was correct or not.

17 i f containsEven:

18 p r i n t ("The list contained an even number")

19 e l s e :

20 p r i n t ("The list did not contain an even number")

The code shown in Example 3.8 works by making a guess and then running through the list of possible counter-examples to fix the guess if needed. Notice the if containsEven appears after the for loop. It is not indented under the for loop. This is very important because other wise you would be checking if the property held for the entire list before you have even looked at the entire list.

Practice 3.9 Type this code and run it using step into and over. Make sureyou get the expected output. What would happen in Example 3.8 if the ifcontainsEven statement were indented under the for loop?

Practice 3.9 Type this code and run it using step into and over. Make sureyou get the expected output. What would happen in Example 3.8 if the ifcontainsEven statement were indented under the for loop?Practice 3.10 Imagine you work at a rehabilitation center for those thatsuffer from obsessive-compulsive disorders. You have to write a program thatmonitors your patients by looking for key words in their daily blogs that theyare required to keep. The words are orderly, shopping, repeat, again, gamble,and bid. If any of these words appear in their blog entry then you should print“You really need to talk to someone about this”. Otherwise you can print,“Thanks for updating your blog”. Here is one possible interaction with thisprogram.

Please make your blog entry for today: I am going to eatbreakfast , then I'll make a bid on some items that I'mshopping for.

You really need to talk to someone about this.

Write this program using the guess and check pattern to see if any of thesensored words appear in their blog entry. Your blog entry will appear on thefirst line only. It was wrapped around to fit on the page here.

 
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