计算机小菜鸟 ~ 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 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败