Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
7.5. There’s a Pattern Here!¶
There’s a pattern in these programs, a pattern that is common when processing data. We call this the Accumulator Pattern. In the first program above, we accumulated the values into the variable
sum. In the last few programs, we accumulated a product into the variable
Here are the five steps in this pattern.
Set the accumulator variable to its initial value. This is the value we want if there is no data to be processed.
Get all the data to be processed.
Step through all the data using a
forloop so that the variable takes on each value in the data.
Combine each piece of the data into the accumulator.
Do something with the result.
7.6. Using the Accumulator Pattern¶
What is the sum of all the numbers between 1 and 100? We can answer that easily using our pattern.
range function has one more version that we can use here. By providing three input numbers, we can specify the start value, the ending value (which is one more than the last value), and the step – how much to change between numbers.
Now let’s answer a slightly harder question: What is the sum of all the even numbers between 0 and 100? It’s easy with our pattern.
How do we know what’s really going on in this program? How do we know that number is taking on all of the even values from 0 to 100? One way we can tell is by using a CodeLens on a smaller problem from 0 to 20. We can step through the program line-by-line, or race to the end by clicking the Last button and then step backwards.
The following is the correct code for printing the sum of all the odd numbers from 1 to 100 using the accumulator pattern, but it is mixed up. Drag the blocks from the left and put them in the correct order on the right. <b>Remember that the statements in the body of a loop must be indented!</b> To indent a block drag it further right. Click the <i>Check Me</i> button to check your solution.</p>
The following is the correct code for printing the sum of all the even numbers from 50 to 100 using the accumulator pattern, but it is mixed up and <b>contains one extra block</b>. Drag the required blocks from the left and put them in the correct order on the right. Don’t forget to indent blocks in the body of the loop. Just drag the block further right to indent. Click the <i>Check Me</i> button to check your solution.</p>
Discuss topics in this section with classmates.