Answer to Question #123316 in Python for Ripley

Question #123316
You can open and close the input file twice. This is just a recommendation. You’re welcome to find a more effective way.For the first time when you open the input file, you can get the total of all grades and the counter in order to calculate the average. You will needoA for loop or a while loop to read a grade at a time: e.g., for line in infile:For the second time when you open the input file, you can compare each grade with the average to determine if the grade is above or below the average. You will need oA for loop or a while loop to read a grade at a time: e.g., for line in infile:oConversion from a line (string) into a float: e.g., grade = float(line)oA condition structure to determine if the grade is above or below the average: e.g., if-elif statementoA counter for the number of grades above the averageoAnother counter for the number of grades below the average
1
Expert's answer
2020-06-22T14:56:56-0400
if __name__ == "__main__":
    grades = []
    average = 0
    with open('input.txt', 'r') as file:
        for line in file:
            grades.append(float(line))
            average += float(line)
    average /= len(grades)

    above_average = 0
    below_average = 0

    for grade in grades:
        if grade > average:
            above_average += 1
        elif grade < average:
            below_average += 1

    print("Above average grade: " + str(above_average))
    print("Below average grade: " + str(below_average))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS