2015-12-13 59 views
0

在Python中,你可以重复码5次通过插入 线→在范围(0,5)计数: 符合此必须被缩进的代码。 编写程序,输入百分比 分数的等级,为每个分配等级:0-20,E ... 81-100,A 打印每个等级有多少,平均分数为 ,以及最高和最低分数。Python。请帮我添加和最大和最小值

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(分数)不起作用。 我不知道如何获得平均分数以及最高和最低分数。 请帮忙。

+0

你发现'score'仅仅是最后输入的号码?在单个数字上进行这些计算是没有意义的。提示:您需要将每个新的“分数”添加到像“list”这样的东西。然后你可以在那个'list'上做这些计算。 – TigerhawkT3

+0

抱歉,在哪里以及需要输入什么来制作列表。 – AAA

+2

这肯定会在你的教科书或其他课程材料。 – TigerhawkT3

回答

-1

您可以创建和填充列表与:

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

它不工作.... – AAA

+0

没有我的代码做的工作。它会创建一个值为0到10的列表。您可以使用'print my_list'来验证它。如果你的代码不适用于列表,你应该对你的答案更具体。 – Randrian

+0

它出来用很奇怪的答案 – AAA

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块中获得总分,最高分和最低分。 :)