Using the break statement to avoid repeating code in the class-average program.

total = 0 # sum of grades
gradeCounter = 0 # number of grades entered
while 1:
grade = raw_input( "Enter grade, -1 to end: " )
grade = int( grade )
# exit loop if user inputs -1
if grade == -1:
break
total += grade
gradeCounter += 1
if gradeCounter != 0:
average = float( total ) / gradeCounter
print "Class average is", average
else:
print "No grades were entered"
Related examples in the same category