首页 > 解决方案 > 如何使用学生的功能来排序他们的平均值?

问题描述

这是问题:

使用程序函数获取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()

标签: pythonsorting

解决方案


这显然是功课,但这是一个开始:

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

看看你是否可以改变排序的顺序。祝你好运!


推荐阅读