python - 如何使用学生的功能来排序他们的平均值?
问题描述
这是问题:
使用程序函数获取5名学生的姓名和4分,最后按平均分的顺序打印出学生的姓名。
我写了函数,所以我可以得到每个学生的平均值,但我不知道如何对学生的名字进行排序。
_firstname = []
_familiname = []
_scores = []
def student1():
input('Please enter the firstname:')
input('Please enter the familiname:')
for i in range(0, 4):
scores = int(input('Please enter the scores:'))
_scores.append(scores)
b = sum(_scores)
avg1 = b/4
print('avg is',avg1)
def student2():
input('Please enter the firstname:')
input('Please enter the familiname:')
for i in range(0, 4):
scores = int(input('Please enter the scores:'))
_scores.append(scores)
b = sum(_scores)
avg2 = b/4
print('avg is',avg2)
def student3():
input('Please enter the firstname:')
input('Please enter the familiname:')
for i in range(0, 4):
scores = int(input('Please enter the scores:'))
_scores.append(scores)
b = sum(_scores)
avg3 = b/4
print('avg is',avg3)
def student4():
input('Please enter the firstname:')
input('Please enter the familiname:')
for i in range(0, 4):
scores = int(input('Please enter the scores:'))
_scores.append(scores)
b = sum(_scores)
avg4 = b/4
print('avg is',avg4)
def student5():
input('Please enter the firstname:')
input('Please enter the familiname:')
for i in range(0, 4):
scores = int(input('Please enter the scores:'))
_scores.append(scores)
b = sum(_scores)
avg5 = b/4
print('avg is',avg5)
student1()
student2()
student3()
student4()
student5()
解决方案
这显然是功课,但这是一个开始:
scores = {90:'best', 80:'good', 60:'ehh'}
ordered_scores = sorted(scores.keys())
print('ordered_scores', ordered_scores)
for score in ordered_scores:
print('score', score, 'student', scores[score])
给你
ordered_scores [60, 80, 90]
score 60 student ehh
score 80 student good
score 90 student best
看看你是否可以改变排序的顺序。祝你好运!
推荐阅读
- powershell - 使用powershell登录后如何从网站获取表格数据?
- c# - EPPlus 忽略小数秒?
- php - 我应该如何静态访问此方法?
- javascript - 如何使用 Webpack 4 为 React 16 生成默认 index.html?
- django - 将依赖模型中的特定对象关联到 Django 中另一个模型的每一行
- javascript - JavaScript for...of、for...in 或其他迭代方法不适用于多个 API 调用
- angular-cli - 角度 cli disableHostCheck true 不起作用
- python - 如何在不展平的情况下迭代元组的元组
- java - 如何让 JFreeChart 在添加数据时更新图表?
- python - 一个接一个地连接数据帧