练不出腹肌不改名! 2021-12-03 00:00 采纳率: 100%
浏览 61
已结题

想问问这道编程,用Python怎么解决

img

  • 写回答

1条回答 默认 最新

  • 偷窃月亮的贼 2021-12-03 10:13
    关注
    import math
    
    
    class Student(object):
        def __init__(self, id, name, chinese, math, english):
            self.id = id
            self.name = name
            self.chinese = chinese
            self.math = math
            self.english = english
            self.ave_score = (chinese+math+english)/3.0
    
    
    def verifyInput(item, type):
        '''验证成绩是否是在合法的输入范围内
    
        如果不合法就继续输入
    
        Args:
            item(int):学生编号
            type(str):课程类型
    
        Returns:
            score:对应科目的成绩
        '''
        score = float(input('请输入第{}个学生的{}成绩:'.format(item, type)))
        while score < 0 or score > 100:
            score = float(input('输入有误,请重新输入第{}个学生的{}成绩:'.format(item, type)))
        return score
    
    
    if __name__ == '__main__':
        # 定义学生列表
        students = list()
    
        # 输入学生成绩
        for item in range(1, 6):
            id = int(input('请输入第{}个学生的学号:'.format(item)))
            name = str(input('请输入第{}个学生的姓名:'.format(item)))
            chinese = verifyInput(item,'语文')
            math = verifyInput(item, '数学')
            english = verifyInput(item, '英语')
            students.append(Student(id, name, chinese, math, english))
    
        # 排序
        students.sort(key=lambda x: x.ave_score, reverse=True)
    
        # 新建list记录各科总和
        score = [0, 0, 0]
    
        print("学号\t姓名\t语文\t数学\t英语\t学生平均成绩")
        for student in students:
            score[0] += student.chinese
            score[1] += student.math
            score[2] += student.english
    
            print(
                "{:2}\t{:3}\t{:3}\t{:3}\t{:3}\t{:3} ".format(
                    student.id, student.name, student.chinese, student.math, student.english, student.ave_score))
    
        # 计算各科平均成绩
        score = [i/5 for i in score]
        print("课程平均分\t{:3}\t{:3}\t{:3}".format(*score))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月15日
  • 已采纳回答 12月7日
  • 创建了问题 12月3日

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误