# 3.10 Program Development: Sequence-Controlled Iteration¶

- Most challenging part of solving a problem on a computer is developing an algorithm for the solution.
- Once a correct algorithm has been specified, creating a working Python program from the algorithm is typically straightforward.

## 3.10.1 Requirements Statement¶

- A requirements statement describes what a program is supposed to do, but not how the program should do it.
- Consider the following simple requirements statement:

  > A class of ten students took a quiz. Their grades (integers in the range 0 – 100) are 98, 76, 71, 87, 83, 90, 57, 79, 82, 94. Determine the class average on the quiz.

- Once you know the problem’s requirements, you can begin creating an algorithm to solve it. Then, you can implement that solution as a program.
- The algorithm for solving this problem must:
  - Keep a running total of the grades.
  - Calculate the average—the total of the grades divided by the number of grades.
  - Display the result.

## 3.10.2 Pseudocode for the Algorithm¶

Set total to zero
Set grade counter to zero
Set grades to a list of the ten grades

For each grade in the grades list:

> Add the grade to the total
> Add one to the grade counter

Set the class average to the total divided by the number of grades
Display the class average

- Note the mentions of total and grade counter.
- We'll use these in the script to calculate the average.
- Variables for totaling and counting normally are initialized to zero.

## 3.10.3 Coding the Algorithm in Python¶

    # fig03_01.py
    """Class average program with sequence-controlled iteration."""

    # initialization phase
    total = 0  # sum of grades
    grade_counter = 0
    grades = [98, 76, 71, 87, 83, 90, 57, 79, 82, 94]  # list of 10 grades

    # processing phase
    for grade in grades:
        total += grade  # add current grade to the running total
        grade_counter += 1  # indicate that one more grade was processed

    # termination phase
    average = total / grade_counter
    print(f'Class average is {average}')

In [1]:

    run fig03_01.py

    Class average is 81.7

### Execution Phases¶

- Initialization phase creates the variables needed to process the grades and set these variables to appropriate initial values.
- Processing phase processes the grades, calculating the running total and counting the number of grades processed so far.
- Termination phase calculates and displays the class average.
- Many scripts can be decomposed into these three phases.

## 3.10.4 Introduction to Formatted Strings¶

- An f-string (short for formatted string) allows inserting values into a string.
- The letter f before the string’s opening quote indicates it’s an f-string.
- You specify where to insert values by using placeholders delimited by curly braces ({ and }).
- `{average}` converts the variable average’s value to a string representation, then replaces `{average}` with that *&replacement text**.
- Replacement-text expressions may contain values, variables or other expressions.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.