2015-12-13 62 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塊中獲得總分,最高分和最低分。 :)