我的老师给我的作业是“写一个python程序来读取3个学生的数学、英语和物理的名字和分数,并根据平均分和成绩按降序打印学生的数据。”他想让我们用这些命令(循环、if语句、打印、输入)无函数
我被困在这里了
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, "%")如何在循环中比较输入的值?
发布于 2021-01-17 22:55:20
您必须将所有值保存在一个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)发布于 2021-01-17 23:02:36
嗯,首先,你必须收集名字和分数,然后进行比较。
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}")https://stackoverflow.com/questions/65762085
复制相似问题