计算机小菜鸟 ~ 2022-08-28 19:27 采纳率: 58.8%
浏览 100
已结题

请问如何编写python成绩排序

该怎么在下面的代码里添加一个可以升序和降序的成绩排序功能呀

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

  • 写回答

4条回答 默认 最新

  • 艾秋 2022-08-28 21:40
    关注
    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 sort_sum(db):
        l = list()
        d = dict()
        for i in db:
            d[i['studentID']] = i['score_1'] + i['score_2'] + i['score_3']
        
        # 升序
        l.append(sorted(d.items(), key=lambda s:s[1]))
        # 降序
        l.append(sorted(d.items(), key=lambda s:s[1], reverse=True))
        return l
    
    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(sort_sum(db))
                continue
            else:
                print('  - Inputting WRONG!')
                continue
     
     
    if __name__ == '__main__':
        db_stat([])
        pass
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月28日
  • 已采纳回答 8月28日
  • 创建了问题 8月28日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么