【问题标题】:" write a python program to marks of 3 students and print student's data in descending order based on average and grade."“编写一个python程序来记录3个学生的分数,并根据平均分和成绩按降序打印学生的数据。”
【发布时间】:2021-01-17 14:51:08
【问题描述】:

我的老师给我这个作业 “编写一个python程序来读取3名学生的数学、英语和物理的姓名和分数,并根据平均和成绩按降序打印学生的数据。” 他希望我们用这些命令来编写它(循环,如果语句,打印,输入) 没有功能

我被困在这里

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, "%")

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

【问题讨论】:

  • 您好,您现在已经有了答案,您可以考虑accepting an answer 来奖励给您最有帮助的评论。

标签: python loops


【解决方案1】:

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

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)

【讨论】:

  • print 中的* 运算符是什么?
  • @IgnacioAlorre 它扩展列表,将value 的每个元素单独打印,而不是将列表打印一次。尝试有无你会看到;)
  • 谢谢,我会的!
  • for v in [('suhail',3,4),('sumesh',5,6)]: print(*v)
【解决方案2】:

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

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}")

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多