While loop with sentinel-controlled repetiton.
total = 0
gradeCounter = 0
grade = raw_input( "Enter grade, -1 to end: " ) # get one grade
grade = int( grade ) # convert string to an integer
while grade != -1:
total = total + grade
gradeCounter = gradeCounter + 1
grade = raw_input( "Enter grade, -1 to end: " )
grade = int( grade )
if gradeCounter != 0:
average = float( total ) / gradeCounter
print "Class average is", average
print "No grades were entered"
Related examples in the same category