首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“写一个python程序给3个学生的分数,并根据平均分和成绩按降序打印学生数据。”

“写一个python程序给3个学生的分数,并根据平均分和成绩按降序打印学生数据。”
EN

Stack Overflow用户
提问于 2021-01-17 22:51:08
回答 2查看 299关注 0票数 0

我的老师给我的作业是“写一个python程序来读取3个学生的数学、英语和物理的名字和分数,并根据平均分和成绩按降序打印学生的数据。”他想让我们用这些命令(循环、if语句、打印、输入)无函数

我被困在这里了

代码语言:javascript
复制
for i in range(0, 3):
    name = input("enter the student's name: ")
    math = int(input("enter the Math mark: "))
    eng = int(input("enter the English mark: "))
    ph = int(input("enter the Physics mark: "))
    Av = ((math + eng + ph) / 3)
    print(name, Av, "%")

如何在循环中比较输入的值?

EN

回答 2

Stack Overflow用户

发布于 2021-01-17 22:55:20

您必须将所有值保存在一个list中,以便能够对它们进行排序,以便以后打印它们

代码语言:javascript
复制
values = []
for i in range(0, 3):
    name = input("enter the student's name: ")
    math = int(input("enter the Math mark: "))
    eng = int(input("enter the English mark: "))
    ph = int(input("enter the Physics mark: "))
    values.append((name, math, eng, ph, (math + eng + ph) / 3))

values.sort(key=lambda x: x[4], reverse=True)
for row in values:
    print(*row)
票数 1
EN

Stack Overflow用户

发布于 2021-01-17 23:02:36

嗯,首先,你必须收集名字和分数,然后进行比较。

代码语言:javascript
复制
from statistics import mean

student_scores = {}

for i in range(0, 3):
    name = input("enter the student's name: ")
    math = int(input("enter the Math mark: "))
    eng = int(input("enter the English mark: "))
    ph = int(input("enter the Physics mark: "))

    student_scores[name] = mean([math, eng, ph])

for name, avg_score in sorted(stduent_scores.items(), key=lambda d: d[1], reverse=True):
    print(f"{name}: {avg_score}")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65762085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档