m0_71136242 2022-05-19 14:15 采纳率: 100%
浏览 85
已结题

请优化此代码,实在不会了

img


此代码已绞尽我的脑汁,修改多次并无显著成效,总有某一处有问题

  • 写回答

4条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-05-19 15:57
    关注

    原代码少了几行,补上就行了,然后格式用制表符调整:

    studentid = {}
    studentscore = {}
    course = set()
    while True:
        line = input()
        if line == 'END':
            break
        words = line.split()
        if words[-1].isnumeric():
            if int(words[-1])>100 or int(words[-1])<0:
                print('成绩无效,请重新输入')
                continue
            score = studentscore.get(words[0])
            if score == None:
                score = {}
            score[words[1]] = words[2]
            studentscore[words[0]] = score
            course.add(words[1])
        else:
            if len(words[0])!= 11:
                print('学号无效,请重新输入')
                continue
            studentid[words[0]] = words[1]
    coursename = list(course)
    print('学号\t姓名',end='')
    for name in coursename:
        print('\t'+name,end='')
    print('\t平均分')
    for id in studentid.keys():
        print(id+'\t'+studentid[id],end='')
        score = studentscore[studentid[id]]
        sum = 0
        cnt = 0
        for name in coursename:
            print('\t',end='')
            if name in score:
                print(score[name],end='')
                sum += int(score[name])
                cnt += 1
        print('\t'+str(int(sum/cnt)))
    

    输出:

    张三 语文 101
    成绩无效,请重新输入
    张三 语文 100
    1001 张三
    学号无效,请重新输入
    10000000001 张三
    张三 数学 95
    李四 数学 98
    李四 语文 90
    10000000002 李四
    END
    学号            姓名    数学    语文    平均分
    10000000001     张三    95      100     97
    10000000002     李四    98      90      94
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月27日
  • 已采纳回答 5月19日
  • 创建了问题 5月19日

悬赏问题

  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆