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.
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
# 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}')
run fig03_01.py
{average} converts the variable average’s value to a string representation, then replaces {average} with that *&replacement text**. ©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.