希望这会有所帮助:
# list of marks
marks = []
# get marks from user
while True:
mark = input("Enter a mark (0-100) <-1 to exit> ")
if mark < 0:
break
elif mark <= 100:
marks.append(mark)
# count number of classes failing
failing = len([f for f in marks if f<50])
best = max(marks)
worst = min(marks)
# check if a least one mark entered
if (len(marks) > 0):
print "The number of classes you are failing:",failing
print "Your best class score:",best
print "Your worst class score",worst
else:
print "Your are not taking any classes!"
演示:
$ python classes.py
Enter a mark (0-100) <-1 to exit> -1
Your are not taking any classes!
$ python classes.py
Enter a mark (0-100) <-1 to exit> 30
Enter a mark (0-100) <-1 to exit> 40
Enter a mark (0-100) <-1 to exit> 50
Enter a mark (0-100) <-1 to exit> 60
Enter a mark (0-100) <-1 to exit> 70
Enter a mark (0-100) <-1 to exit> 80
Enter a mark (0-100) <-1 to exit> -1
The number of classes you are failing: 2
Your best class score: 80
Your worst class score 30