In: Computer Science
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall average test grade. E.g if there are 3 test each student must take and the user enters the following set of test scores for the two students…:
30, 40, 50 for the first student
50, 60, 70 for the second student
…then program will print the average for each student (i.e. 40 for the first student and 60 for the second student – the existing program already does this) and additionally will also print the overall average (i.e. (40 + 60) /2 = 50).
------------
this is the provided code from 'examaverages.py' :
## # This program computes the average grade for multiple students. # # Obtain the number of test grades per student. numExams = int(input("How many grades does each student have? ")) # Initialize moreGrades to a non-sentinel value. moreGrades = "Y" # Compute average grades until the user wants to stop. while moreGrades == "Y" : # Compute the average grade for one student. print("Enter the grades out of 100.") total = 0 for i in range(1, numExams + 1) : score = int(input("test %d: " % i)) # Prompt for each test grade. total = total + score average = total / numExams print("The average is %.2f" % average) # Prompt as to whether the user wants to enter grades for another student. moreGrades = input("Enter grades for another student (Y/N)? ") moreGrades = moreGrades.upper()
New Lines: 9,10,13,23,28,29
I have commented the description about the added lines for the modification in the given code.
I have started the comment for the new lines with # (line added).
If you have any queries, please ask in the comments, I regularly check them for 2-3 days.
Thank You.
Code:
# This program computes the average grade for multiple students.
#
# Obtain the number of test grades per student.
numExams = int(input("How many grades does each student have? "))
# Initialize moreGrades to a non-sentinel value.
moreGrades = "Y"
studs = 0 #(line added)
avgTotal = 0 #(line added)
# Compute average grades until the user wants to stop.
while moreGrades == "Y" :
studs = studs + 1 #(line added) counts the number of students
# Compute the average grade for one student.
print("Enter the grades out of 100.")
total = 0
for i in range(1, numExams + 1) :
score = int(input("test %d: " % i)) # Prompt for each test grade.
total = total + score
average = total / numExams
print("The average is %.2f" % average)
avgTotal = avgTotal + average #(line added) Calculates the sum of the averages for all students
# Prompt as to whether the user wants to enter grades for another student.
moreGrades = input("Enter grades for another student (Y/N)? ")
moreGrades = moreGrades.upper()
ovrAvg = avgTotal/studs #(line added) Calculates the overall average
print("The overall average for all students is: " + str(ovrAvg)) #(line added) Prints the overall average