直接上代碼,解釋在注釋,不懂評論留言,必回
'''
4 輸入n個學生的3門成績,
分別計算n個學生的總分,每門功課的平均分,
按學生的總分降序顯示學生的資訊,并輸出每門功課的平均分
'''
print("請輸入一共多少個學生: ",end="")
n = int(input())
#創建二維陣列
a =[[]for i in range(n)]
b =[[]for i in range(n)]
c =[[]for i in range(n)]
for i in range(n):
for j in range(3):
if j == 0:
print("請輸入第{}個學生的第一門成績:".format(i+1),end="")
a[i].append(float(input()))
c[j].append(a[i][j])
elif j == 1:
print("請輸入第{}個學生的第二門成績:".format(i+1),end="")
a[i].append(float(input()))
c[j].append(a[i][j])
else:
print("請輸入第{}個學生的第三門成績:".format(i+1),end="")
a[i].append(float(input()))
c[j].append(a[i][j])
b[i].append('第{}個學生的總成績'.format(i+1))
b[i].append(sum(a[i]))
#按照序列1降序
b.sort(key = lambda x:x[1],reverse = True)
#轉化為字典輸出,也可以不轉化,直接輸出b
d={}
for i in b:
d[i[0]] = i[1]
print()
print("列印降序之后的學生總成績排名:")
print(d)
print()
for i in range(3):
print("第{}門成績的平均分:".format(i+1),sum(c[i])/3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/175266.html
標籤:其他
