蟒蛇。请帮我添加最大值和最小值

时间:2015-12-13 12:23:05

标签: python

在Python中,您可以通过插入重复代码5次 线→用于范围内的计数(0,5): 然后必须缩进此后的代码。 编写一个程序来输入一个百分比类 分数,为每个分配等级:0-20,E ... 81-100,A 打印每个等级的数量, 平均分,最高和最低分。

A=0
B=0
C=0
D=0
E=0
for count in range(0,5):
    score = int(input("Type your class students' score."))
    if score >81:
        print("A")
        A=A+1
    elif score>61:
        print("B")
        B=B+1 
    elif score>41:
        print("C")
        C=C+1
    elif score>21:
        print("D")
        D=D+1
    else:
        print("E")
        E=E+1
print "There are",A,"number of A"
print "There are",B,"number of B"
print "There are",C,"number of C"
print "There are",D,"number of D"
print "There are",E,"number of E"
totalscore = sum(score)
highestscore = max(score)
lowestscore = min(score)
print "Average score is",totalscore/5
print "The highest score is",highestscore
print "The lowest score is",lowestscore

我已经完成了,但它并没有从totalscore = sum(得分)开始工作。 我不知道如何获得平均分数以及最高和最低分数。 请帮忙。

2 个答案:

答案 0 :(得分:1)

import sys
from collections import Counter

grade_counter = Counter()
sum_score, highest_score, lowest_score = 0, 0, sys.maxint
TIMES = 5


class RangeDict(dict):

    def __getitem__(self, key):
        for k in self.keys():
            if k[0] < key <= k[1]:
                return super(RangeDict, self).__getitem__(k)
        raise KeyError

grade_range = RangeDict({
    (81, 100): "A",
    (61, 81): "B",
    (41, 61): "C",
    (0, 41): "E"
})

for i in range(TIMES):
    score = int(raw_input("Type your class students' score."))
    grade = grade_range[score]
    print grade
    grade_counter[grade] += 1
    sum_score += score
    if score > highest_score:
        highest_score = score
    if score < lowest_score:
        lowest_score = score


for grade in sorted(grade_counter):
    print "There are  %s number of A %d" % (
        grade, grade_counter[grade])

print "Average score is", sum_score / TIMES
print "The highest score is", highest_score
print "The lowest score is", lowest_score

首先,我认为您可以使用collections.Counter记录成绩计数。然后,可以在for块中获得总分,最高分和最低分。 :)

答案 1 :(得分:-1)

您可以使用以下内容创建和填充列表:

my_list = []
for i in range(10):
   my_list.append(i)