该怎么在下面的代码里添加一个可以升序和降序的成绩排序功能呀
from search import search_by_id
stat_submenu = '''
5 统计 ---|--- 1 学生总分
|--- 2 单科总成绩、平均分
|--- 3 单科最高分
|--- 4 总成绩排序
|--- 0 返回
'''
def scores_sum(db, sid):
sum = 0
if search_by_id(db, sid) == {}:
print(' - %s does not exist.' % sid)
sum = -1
else:
for i in db:
if i['studentID'] == sid:
sum = i['score_1'] + i['score_2'] + i['score_3']
return sum
def object_sum(db, object_n):
avg = 0
sum = 0
total = len(db)
for i in db:
sum += float(i[object_n])
avg = sum / total
return sum, avg
def object_max(db, object_n):
max = 0
for i in db:
temp = float(i[object_n])
if temp > max:
max = temp
return max
def db_stat(db):
temp_db = db
while True:
print(stat_submenu)
option = input(' > Enter your option:')
if option == '0':
break
elif option == '1':
sid = input(' >> Student ID:')
sum = scores_sum(db, sid.strip())
if sum != -1:
print(' - Sum_Score of %s is %f' % (sid, sum))
continue
elif option == '2':
object_name = input(' > Object name:')
sum, avg = object_sum(db, object_name.strip())
print(' - Sum of the object %s is %f' % (object_name, sum))
print(' - Average of the object %s is %f' % (object_name, avg))
continue
elif option == '3':
object_name = input(' > Object name:')
max = object_max(db, object_name.strip())
print(' - The maximum of the object %s is %f' % (object_name, max))
continue
elif option == '4':
print(' - To be coded.')
continue
else:
print(' - Inputting WRONG!')
continue
if __name__ == '__main__':
db_stat([])
pass