lecIerc 2022-10-19 13:15 采纳率: 100%
浏览 56
已结题

在这个代码的基础上怎么完成后面几点呢?(语言-python)

(1)请使用上述结构输入5个学生的name及3门课的成绩并存储到列表scores中。

(2)计算每位同学的平均成绩,同时在字典中添加关键字“avg”用来表示平均成绩。

(3)按照每个学生的平均成绩由高到低排序输出5个学生的相关信息。

(4)遍历列表scores,将每门课不及格的学生名单进行打印。

scores=[]
for i in range(5):
    keys = ['name', 'english', 'python', 'math']
    strstu = input("请输入学生姓名,english,python,math三个科目的成绩分别以空格隔开:\n")
    ls = strstu.split(" ")
    dictionary = dict(zip(keys, ls))
    scores.append(dictionary)
print(scores)
  • 写回答

2条回答 默认 最新

  • ·星辰大海 2022-10-19 13:26
    关注
    
    scores=[]
    for i in range(5):
        keys = ['name', 'english', 'python', 'math']
        strstu = input("请输入学生姓名,english,python,math三个科目的成绩分别以空格隔开:\n")
        ls = strstu.split(" ")
        dictionary = dict(zip(keys, ls))
        dictionary['avg']=round((int(dictionary['english'])+int(dictionary['python'])+int(dictionary['math']))/3,1)
        scores.append(dictionary)
    scores.sort(key=lambda x:x['avg'],reverse=True)
    for i in scores:
        print(i)
    print("不及格:")
    for i in scores:
        if int(i['english'])<60:
            print('英语不及格:',i)
        if int(i['python'])<60:
            print('python不及格:',i)
        if int(i['math'])<60:
            print('数学不及格:',i)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • honestman_ 2022-10-19 13:28
    关注
    
    # scores=[]
    # for i in range(5):
    #     keys = ['name', 'english', 'python', 'math']
    #     strstu = input("请输入学生姓名,english,python,math三个科目的成绩分别以空格隔开:\n")
    #     ls = strstu.split(" ")
    #     dictionary = dict(zip(keys, ls))
    #     scores.append(dictionary)
    scores = [{'name': 'hahah', 'english': '11', 'python': '22', 'math': '33'}, {'name': 'hh', 'english': '33', 'python': '33', 'math': '33'}, {'name': 'kk', 'english': '22', 'python': '22', 'math': '33'}, {'name': 'll', 'english': '33', 'python': '33', 'math': '11'}, {'name': 'll', 'english': '33', 'python': '33', 'math': '11'}]
    for score in scores:
        score['avg'] = (int(score['english']) + int(score['python']) + int(score['math']))/3
    scores = sorted(scores, key=lambda x:x['avg'])
    for score in scores:
        print(f'学生{score["name"]}的平均分数为{score["avg"]}')
    print('英语不及格的同学为:')
    for score in scores:
        if int(score['english']) < 60:
            print(score['name'])
    print('python不及格的同学为:')
    for score in scores:
        if int(score['python']) < 60:
            print(score['name'])
    print(f'数学不及格的同学为:')
    for score in scores:
        if int(score['math']) < 60:
            print(score['name'])
    
    

    问题解决的话记得点击一下采纳谢谢

    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图
  • ¥15 Python爬取交通拥堵指数数据
  • ¥15 使用vba抓取重定向网页问题
  • ¥20 付费需求测试程序(细谈)。
  • ¥15 为什么这段c++代码会报这么多语法错误?
  • ¥20 如何利用C语言实现用最小二乘法选配两个经验公式
  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
  • ¥30 遇到一个的问题,请教各位