# 3.12 Program Development: Nested Control Statements¶

### Requirements statement:¶

> A college offers a course that prepares students for the state licensing exam for real-estate brokers. Last year, several of the students who completed this course took the licensing examination. The college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam and a 2 if the student failed.

> Analyze the results of the exam as follows:

> > Input each test result (i.e., a 1 or a 2). Display the message “Enter result” each time the program requests another test result.

> > Count the number of test results of each type.

> > Display a summary of the test results indicating the number of students who passed and the number of students who failed.

> > If more than eight students passed the exam, display “Bonus to instructor.”

- Observations about the problem:
  - The program must process 10 test results. We’ll use a for statement and the range function to control iteration.
  - Each test result is a number—either a 1 or a 2. Each time the program reads a test result, the program must determine if the number is a 1 or a 2. We test for a 1 in our algorithm. If the number is not a 1, we assume that it’s a 2.
  - We’ll use two counters—one to count the number of students who passed the exam and one to count the number of students who failed.
  - After the script processes all the results, it must decide if more than eight students passed the exam so that it can bonus the instructor.

### Top-Down, Stepwise Refinement¶

- Pseudocode representation of the top:

  > Analyze exam results and decide whether instructor should receive a bonus

- First Refinement

  > Initialize variables
  > Input the ten exam grades and count passes and failures
  > Summarize the exam results and decide whether instructor should receive a bonus

- Second Refinement

  - Commit to specific variables.
  - Need counters to record the passes and failures, and a variable to store the user input.
  - Initialize variables can be refined as follows:

    > > Initialize passes to zero
    > > Initialize failures to zero

- Input the ten exam grades and count passes and failures requires a loop that successively inputs the result of each exam.
- We know there are ten exam results, so the `for` statement and the `range` function are appropriate.
- The refinement is

> For each of the ten students

> > Input the next exam result If the student passed
> > Add one to passes
> > Else
> > Add one to failures

- Summarize the exam results and decide whether instructor should receive a bonus may be refined as follows:

  > Display the number of passes
  > Display the number of failures If more than eight students passed
  > Display “Bonus to instructor”

### Complete Pseudocode Algorithm¶

> Initialize passes to zero
> Initialize failures to zero
> For each of the ten students

> > Input the next exam result
> > If the student passed
> > Add one to passes

> > Else
> > Add one to failures

> Display the number of passes
> Display the number of failures
> If more than eight students passed
> Display “Bonus to instructor”

### Implementing the Algorithm¶

    # fig03_03.py
    """Using nested control statements to analyze examination results."""

    # initialize variables
    passes = 0  # number of passes
    failures = 0  # number of failures

    # process 10 students
    for student in range(10):
        # get one exam result
        result = int(input('Enter result (1=pass, 2=fail): '))

        if result == 1:
            passes = passes + 1
        else:
            failures = failures + 1

    # termination phase
    print('Passed:', passes)
    print('Failed:', failures)

    if passes > 8:
        print('Bonus to instructor')

In [1]:

    run fig03_03.py

    Passed: 5
    Failed: 5

In [2]:

    run fig03_03.py

    Passed: 9
    Failed: 1
    Bonus to instructor

©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.