【问题标题】:Python; calculate the average of three grades for each of a number of studentsPython;计算每个学生的三个成绩的平均值
【发布时间】:2016-01-02 19:13:09
【问题描述】:

我必须编写代码来计算每个学生的三个平均成绩,并根据得到的平均成绩显示一条消息。

程序需要能够处理任意数量的学生;但每个学生只有 3 个等级。我必须使用一种方法来计算每个学生的平均分数,并根据平均值确定适当的消息。

这是我到目前为止的代码,我被卡住了:

def main():
    more = 'y'

    while more == 'y' and more == 'Y':
        numScore = int(input('How many test score per student: '))

    for numtest in range(numScore):
        print ("The score for test")
        score = int(input(': '))

        total += score

    total = 0

    print ("Student number", students + 1)
    print ('-----------------------------------------')

    avg = getAvg(numScore)

    if avg > 90:
        print ("You're doing excellent work")
    elif avg > 85 and avg <= 90:
        print("You are doing pretty good work")
    elif avg > 70 and avg <= 85:
        print ("You better get busy")
    else:
        print ("You need to get some help")

def getAvg (numScore):
    avg = total / numScore

    print ("The average for student number", student + 1, \ "is:", avg)

 more = input('Do you want to enter another student test score and get the average score of the student (Enter y for yes and n for no): ')

main()

【问题讨论】:

  • 计算每个学生三个成绩的平均值,并根据得到的平均成绩显示一条消息。该程序需要能够处理任意数量的学生;但每个学生只有 3 个年级。
  • 你有什么问题?
  • 请使用edit 链接正确格式化您的代码,并在问题正文中提出实际问题。
  • 条件“more == 'y' and more == 'Y'”永远不会为真,因为它需要“more”同时保持 2 个不同的值。
  • 我必须编写一个代码,它将:计算每个学生的三个成绩的平均值,并根据得到的平均成绩显示一条消息。该程序需要能够处理任意数量的学生;但每个学生只有 3 个等级。我必须使用方法来计算每个学生的平均分数,并根据平均值阻止适当的消息。这是我到目前为止的代码,我是股票

标签: python python-3.x


【解决方案1】:

您的脚本中的大部分元素都是正确的,但您需要更多地考虑所有内容的顺序。

确保将所需的任何参数传递给函数。您的getAvg 函数需要三个参数来计算平均值并将其与学号一起显示。你也忘了回average

在计算avg 范围时,如果您首先测试&gt;90,则根据定义,下一个elif avg &gt; 85 将小于或等于90,因此只需随后测试&gt;85 .

以下内容经过重新安排:

def getAvg(student, total, numScore):
    average = total / numScore
    print ("The average for student number", student, "is:", average)
    return average

def main():
    student = 0
    more = 'y'

    while more == 'y':
        student += 1
        total = 0

        numScore = int(input('How many test scores per student: '))

        for test_number in range(numScore):
            print ("The score for test", test_number+1)
            score = int(input(': '))
            total += score

        print ("Student number", student)
        print ('-----------------------------------------')

        avg = getAvg(student, total, numScore)

        if avg > 90:
            print ("You're doing excellent work")
        elif avg > 85:
            print("You are doing pretty good work")
        elif avg > 70:
            print ("You better get busy")
        else:
            print ("You need to get some help")

        print()
        print('Do you want to enter another student test score and get the average score of the student?')
        more = input('Enter y for yes, and n for no: ').lower()

main()

为您提供以下类型的输出:

How many test scores per student: 3
The score for test 1
: 93
The score for test 2
: 89
The score for test 3
: 73
Student number 1
-----------------------------------------
The average for student number 1 is: 85.0
You better get busy

Do you want to enter another student test score and get the average score of the student?
Enter y for yes, and n for no: n

【讨论】:

  • 非常感谢您的帮助
  • 不客气。您现在可以单击答案旁边的勾号以接受它。您还将获得一枚徽章。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多