Table of Contents » Chapter 3 : Processing : Repetition : Repetition Concepts
Repetition Concepts
Contents
- Overview
- Repetition Example 1
- Repetition Example 2
Overview
Repetition in programming allows us to repeat a code block (one or more programming statements) as many times as needed to accomplish a task. Like Decisions, repetition is a very common and useful programming concept. Implementing repetition in Python involves choosing from a couple of repetition options in the language. Also like decisions, repetition operates based on conditions. The first of the repetition options is called a while loop, which repeats a code block until a specified condition is met. The second repetition option is the for loop, which repeats a code block a specified number of times. For example, let's say we want to prompt the user for several numbers and after they have entered them we want to calculate and report the average of those numbers. We could write this task out in English something like this:
1. Ask the user to enter a number
2. Store the number they entered
3. And then repeat the following:
a. Ask the user for the next number
b. Add the new number entered to a running total (we call this an accumulator).
c. Check to see if they are finished, when they are, stop asking for more numbers
4. Next, calculate the average by dividing the running total by the number of numbers the user entered
5. Lastly, print a message for the user informing them of the average of the numbers they entered.
This is an example of repetition, in this case, we repeat asking for the next number until they are "finished," which is our condition. We will cover this and other examples of how to use repetition in this chapter.
Let's prompt the user for a series of test scores and our program will produce the average of the test scores. To calculate an average we add up all of the numbers (scores) in our list to get a total, and then we divide that calculated total by the number of scores we received from the user (in this example, the number of scores).
Here's a description and flowchart of how we might approach this problem:
As depicted in the flowchart, a common approach would be to set a variable that will hold our running total age (we call this an accumulator). Then prompt the user for the first test score. Then in a repetition structure (loop), we add each newly entered score to the accumulator. After adding we prompt the user for the next score. We repeat these steps over and over until the user indicates there are no more scores to enter (we will take a closer look at how the user indicates this on the next pages). When it exits the loop, the accumulator would contain the total score. We could then calculate the average score after the loop by dividing the total score by the number of scores entered by the user.

For our second example, suppose we have a list of the ages of every person in Utah and we want to calculate the average age.
Looking at census data for 2020, the population of Utah was 3,271,616 people. It would be absurd to try to manually calculate the average of that many numbers (ages). So, if we have a list of all the ages (let's say in a file), we could read those numbers into a Python program, add them all up and then divide by 3,271,616. To do this efficiently, we would use a repetition structure.
Here's a description and flowchart of how we might approach this problem:
As depicted in the flowchart, a common approach would be to set a variable that will hold our running total age (we call this an accumulator). Then we read each age from the data source (we'll learn more about how to read data from outside sources later) and in a repetition structure (loop) we add each newly read age to the accumulator. We repeat that step over and over until there are no more pages to read from the data source. In this example, the loop would repeat 3,271,616 times. When it exits the loop, the accumulator would contain the total age. We could then calculate the average age after the loop by dividing the total age by the number of ages.
In the following several pages we will take a look at the while loop and the for loop which are two of the primary repetition structures that we use in programming. We'll also look at several examples of problems that are solved by repetition structures.
