2015-10-06 177 views
-6

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

该计划需要能够处理任何数量的学生;但每个学生只有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() 
+0

计算的三个等级平均为每一批学生和显示器的取决于所得到的平均成绩的消息。该计划需要能够处理任何数量的学生;但每个学生只有3个等级。 – William

+2

你的问题是什么? –

+0

请使用[编辑]链接正确格式化您的代码,并在问题主体中提出实际问题。 – MattDMo

回答

0

你有最脚本正确的元素,但你需要更多地考虑一切的秩序。

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

当计算avg范围,如果为>90第一测试,然后根据定义下一elif avg > 85将小于或等于90所以它仅需要然后测试>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 
+0

非常感谢你的帮助 – William

+0

不客气。你现在可以点击答案来接受它。您也将获得徽章。 –