Develop a class-averaging program that processes an arbitrary number of grades each time the program executes.
Determine the class average for the quiz
Initialize variables
Input, sum and count the quiz grades
Calculate and display the class average
"Initialize variables" can be refined as follows:
Initialize total to zero
Initialize grade counter to zero
"Input, sum and count the quiz grades" can be refined as follows:
Input the first grade (possibly the sentinel)
While the user has not entered the sentinelAdd this grade into the running total
Add one to the grade counter
Input the next grade (possibly the sentinel)
"Calculate and display the class average" can be refined as follows:
If the counter is not equal to zero
Set the average to the total divided by the grade counter
Display the averageElse
Display “No grades were entered”
Complete second refinement:
Initialize total to zero
Initialize grade counter to zero
Input the first grade (possibly the sentinel)
While the user has not entered the sentinelAdd this grade into the running total
Add one to the grade counter
Input the next grade (possibly the sentinel)
If the counter is not equal to zero
Set the average to the total divided by the counter
Display the averageElse
Display “No grades were entered”
# fig03_02.py
"""Class average program with sentinel-controlled iteration."""
# initialization phase
total = 0 # sum of grades
grade_counter = 0 # number of grades entered
# processing phase
grade = int(input('Enter grade, -1 to end: ')) # get one grade
while grade != -1:
total += grade
grade_counter += 1
grade = int(input('Enter grade, -1 to end: '))
# termination phase
if grade_counter != 0:
average = total / grade_counter
print(f'Class average is {average:.2f}')
else:
print('No grades were entered')
run fig03_02.py
while statement. while’s suite. False, the user entered -1, so the suite does not execute. True, the suite executes, adding the grade value to the total and incrementing the grade_counter. while’s condition is tested again. grade is always input immediately before the program tests the while condition. –1 to the total. if…else statement executes.:) and a format specifier that describes how to format the replacement text..2f formats the average as a floating-point number (f) with two digits to the right of the decimal point (.2). while statement is followed immediately by an if…else statement.©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.