1
# Fig. 3.10: fig03_10.py
2
# Class average program with counter-controlled repetition
3
4
#Initialization phase
5
total = 0 # sum of grades
6
counter = 0 # number of grades entered
7
8
# processing phase
9
while counter < 10: # loop 10 times
10
grade = raw_input("Please enter grade of student:") # get one grade
11
grade = int(grade) # convert string to an integer
12
total = total + grade
13
counter = counter + 1
14
15
print "total:", total
16
print "counter:", counter
17
18
# termination phase
19
average = total / counter # integer division
20
print "Class average is: ", average
# Fig. 3.10: fig03_10.py2
# Class average program with counter-controlled repetition3

4
#Initialization phase5
total = 0 # sum of grades6
counter = 0 # number of grades entered7

8
# processing phase9
while counter < 10: # loop 10 times10
grade = raw_input("Please enter grade of student:") # get one grade11
grade = int(grade) # convert string to an integer12
total = total + grade13
counter = counter + 114

15
print "total:", total16
print "counter:", counter17

18
# termination phase19
average = total / counter # integer division20
print "Class average is: ", average 1
# Fig. 3.11: fig03_11.py
2
# Class average program with sentinel-controlled repetition
3
4
#Initialization phase
5
total = 0 # sum of grades
6
counter = 0 # number of grades entered
7
8
grade = raw_input("Please enter grade of student:")
9
if grade == "" or grade =="end":
10
print "no grades were entered!"
11
else:
12
grade = int(grade)
13
total = total + grade
14
counter = counter + 1
15
while grade <> "end":
16
grade = raw_input("Please enter grade of student:")
17
if grade <> "end":
18
grade = int(grade)
19
total = total + grade
20
counter = counter + 1
21
22
print "total:", total
23
print "counter:", counter
24
average = total / counter
25
print "Class average is: ", average
# Fig. 3.11: fig03_11.py2
# Class average program with sentinel-controlled repetition3

4
#Initialization phase5
total = 0 # sum of grades6
counter = 0 # number of grades entered7

8
grade = raw_input("Please enter grade of student:")9
if grade == "" or grade =="end":10
print "no grades were entered!"11
else:12
grade = int(grade)13
total = total + grade14
counter = counter + 115
while grade <> "end":16
grade = raw_input("Please enter grade of student:")17
if grade <> "end":18
grade = int(grade)19
total = total + grade20
counter = counter + 121

22
print "total:", total23
print "counter:", counter24
average = total / counter25
print "Class average is: ", average
浙公网安备 33010602011771号