我必须编写代码来计算每个学生的三个等级的平均值,并根据得到的平均成绩显示一条消息。
该计划需要能够处理任意数量的学生;但每个学生只有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 :(得分: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