A loop that accumulates a total as it reads each number from a series is often said to keep a what?

5.8 Rewrite the following code so it calls the range function instead of using the list for x in (0 r 1, 2, 3, 4 , 5]: print 'I love to program!'

5.9 What will the following code display?

for number in range(6): print number

5.10 What will the following code display?

for number in range(2, 6) : print number

5.11 What will the following code display?

for number in range(0, 501, 100): print number

5.12 What will the following code display?

for number in range(10, 5, -1): print number

5.41 Calculating a Running Total

CONCEPT: A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator.

Many programming tasks require you to calculate the total of a series of numbers. For example, suppose you are writing a program that calculates a business's total sales for a week. The program would read the sales for each day as input and calculate the total of those numbers.

Programs that calculate the total of a series of numbers typically use two elements: •' A loop that reads each number in the series.

• A variable that accumulates the total of the numbers as they are read.

The variable that is used to accumulate the total of the numbers is called an accumulator. It is often said that the loop keeps a running total because it accumulates the total as it reads each number in the series. Figure 5-7shows the general logic of a loop that calculates a running total.

A loop that accumulates a total as it reads each number from a series is often said to keep a what?

When the loop finishes, the accumulator will contain the total of the numbers that were read by the loop. Notice that the first step in the flowchart is to set the accumulator variable to 0. This is a critical step. Each time the loop reads a number, it adds it to the accumulator. If the accumulator starts with any value other than 0, it will not contain the correct total when the loop finishes.

Let's look at a program that calculates a running total. Program 5-13 allows the user to enter five numbers, and it displays the total of the numbers entered.

Program 5-13 (sum_numbers.py}

# This program calculates the sum of

# five numbers entered by the user.

5 # Initialize an accumulator variable, total = 0.0

Continue reading here: Checkpoint

Was this article helpful?

A loop that accumulates a total as it reads each number from a series is often said to keep a what?

Quiz 1 Notes

Quiz 1

Chapter 4 Introduction to Repetition Structures:

A repetition structure causes a statement or set of statements to execute repeatedly.

A condition-controlled loop uses a true/false condition to control the number of times that

it repeats.

A count-controlled loop repeats a specific number of times.

This can be done with a repetition structure, which is more commonly known as a loop.

A condition-controlled loop causes a statement or set of statements to repeat as long as

a condition is true. In Python, you use the while statement to write a condition-controlled

loop.

Each execution of the body of a loop is known as an iteration.

The while loop is known as a pretest loop, which means it tests its condition before per-

forming an iteration.

An infinite loop continues to repeat until the program is interrupted. Infinite loops usually

occur when the programmer forgets to write code inside the loop that makes the test

condition false.

A count-controlled loop iterates a specific number of times. In Python,you use the for

statement to write a count-controlled loop.

Python programmers commonly refer to the variable that is used in the for clause as the

target variable because it is the target of an assignment at the beginning of each loop

iteration.

range(10, 0, −1) In this function call, the starting value is 10, the sequence’s

ending limit is 0, and the step value is 21. This expression will produce the

following sequence: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

A running total is a sum of numbers that accumulates with each iteration of a loop. The

variable used to keep the running total is called an accumulator.

The variable that is used to accumulate the total of the numbers is called an

accumulator.

It is often said that the loop keeps a running total because it accumulates the total as it

reads each number in the series.

A sentinel is a special value that marks the end of a sequence of values.

Input validation is the process of inspecting data that has been input to a program, to

make sure it is valid before it is used in a computation. Input validation is commonly

done with a loop that iterates as long as an input variable references bad data.

For convenience, Python offers a special set of operators designed specifically for these

jobs. Table 4-2 shows the augmented assignment operators.

The first input operation—just before the loop—is called a priming read, and its purpose

is to get the first input value that will be tested by the validation loop.

An input validation loop is sometimes called an error trap or an error handler.

A loop that is inside another loop is called a nested loop.

You can use loops to draw graphics that range in complexity from simple shapes to

elaborate designs.

Which type of loop causes a statement or set of statements to repeat a specific number of times?

A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. A controlled loop contains some repetition condition which will eventually force the looping construct to terminate.

Which pair of loops causes a statement or set of statements to repeat as long as a condition is true?

Which pair of loops causes a statement or set of statements to repeat as long as a condition is true? The While loop is known as a pretest loop, which means it tests its condition before performing an iteration.

What type of loop uses a Boolean expression to control the number of times that it repeats a statement or set of statements?

A condition-controlled or sentinel loop uses a true/false condition to control the number of times that it repeats.

Which pair of loops are considered as posttest loops?

The do-while loop is a posttest loop, which means its expression is tested after each iteration.