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.”
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
Initialize passes to zero
Initialize failures to zero
for statement and the range function are appropriate. 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”
Initialize passes to zero
Initialize failures to zero
For each of the ten studentsInput the next exam result
If the student passed
Add one to passesElse
Add one to failuresDisplay the number of passes
Display the number of failures
If more than eight students passed
Display “Bonus to instructor”
# 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')
run fig03_03.py
run fig03_03.py
©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.