Iterating over sequences can be useful when we want to count something. Counting is a common occurrence in computer programs. We may want to count the number of people who are taking an Introduction to Computer Science, we may want to add up the amount of money made from ticket sales to a concert. The applications of counting could go on and on. To count we can use what is called the Accumulator Pattern. This pattern works by initializing a variable that keeps track of how much we have counted so far. Then we can write a for loop to go through a list of elements in a sequence and add each element's value to the accumulator. The pattern looks like this:

This pattern is pretty abstract. With an example it should make some more sense.

Example 3.11 Here is a program that counts the number of elements in a list.Of course, we could use the len(lst) function to give us the number of elementsin the list, but this illustrates the accumulator pattern for us. This code countsthe number of integers in a list. Actually, it counts the number of whitespaceseparated strings in the list since the code never converts the strings to integers.

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 count = 0 # Here is the beginning of the accumulator pattern

4 f o r e i n lst:

5 count = count + 1

6

7 p r i n t ("There were", count ,"integers in the list.")

The Accumulator pattern can be used in a multitude of ways. It can be used to count by adding one each time through the loop, it can be used to count the number of items that satisfy some constraint. It can be used to add some number of items in the list together. It can be used to compute a product if needed.

Practice 3.12 Modify the code in Example 3.11 to count the number of evenintegers entered by the user.

Practice 3.13 Write a program that asks the user to enter an integer andcomputes the factorial of that integer, usually written n! in mathematics. Thedefinition of factorial says that 0! = 1 and for n > 0, n! = 1 ∗ 2 ∗ 3 . . . ∗ n.You can write this program by using the range function and the accumulatorpattern to multiply all the numbers from 1 to n together. If you need to reviewhow to use the range function you can refer to p. 69.

In the previous exercise it is worth mentioning that if written correctly not only will it compute n! when n > 0, but it will also compute 0! correctly. When 0! is computed, the body of the for loop is not executed at all. Take a look at your code or at the solution to the practice exercise to confirm this. This sometimes happens when writing code and is called a boundary condition. A boundary condition happens when there is a special case that causes the program control to take a slightly different path. In this case, computing 0! is a boundary condition and the body of the for loop is not executed. When testing code you have written it is important that you consider your boundary conditions and that you test them to be sure that your program handles them correctly.

Found a mistake? Please highlight the word and press Shift + Enter