Write a Python program Assignment A professor at Hardtack Un

Write a Python program:

Assignment:

A professor at Hardtack University has an unusual method of grading. The students may or may not all take the same number of tests. The individual tests are weighted, and these weights are used to compute the student\'s average. Important: the weights for all of the tests for a given student must add to 100. Assuming that w1 ... wn are the weights, and g1 ... gn are the grades, the average is computed by the formula:

((w1 * g1) + (w2 * g2) + ... + (wn * gn)) / 100

The names of the students and their grades, with weights, are stored in a text file in the format firstName lastName w1 g1 w2 g2 ... wn gn

Write the program weightedAverage.py. Ask the user to enter the name of a file of grades. Compute and print each student\'s average, using the formula given above. Then compute and print the class average, an average of the individual averages. Format all averages to one decimal place. Note the weights for each student will always add up to 100.

For example, if the text file contains:

Billy Bother 20 89 30 94 50 82

Hermione Heffalump 40 93 60 97

Kurt Kidd 20 88 30 82 40 76 10 99

Then the output might look like this:

Billy Bother\'s average: 87.0

Hermione Heffalump\'s average: 95.4

Kurt Kidd\'s average: 82.5

Class average: 88.3

Solution

def main():

    # Ask the user to input the name of a text file that contains grades
    #   with weights.
    print(\'Welcome! \ This program is designed to read numeric input from a .txt file\ and use it inside of a Python program.\')
    print()
    fileName = input(\'What is the name of the file that you would like to use? \ (Please Include .txt Extension!) \ \\t Name of file: \')
  
    # Create an empty list to hold the values for all of the weighted averages,
    #   so they are easy to average later.
    weightedAveragesList = []
  
    # Open the text file, and designate that it will be used for reading.
    infile = open(fileName, \'r\')
  
    # Print an opening introduction so the user knows that the .txt file is being
    #   read and data is being extracted.
    print (\"\ ###-- \" + fileName + \" --###\ \")
  
    # Loop that will separate values, and empty lists to hold what will become numeric values.
    for line in infile:
        allValues = line.split()
        # Empty list(s) to hold the weight and grade values
        grades = []
        weights = []
        # This is used to reset this variable for each student to 0
        weightedAverage = 0
        # This will take the values pertinent for the weights list, and will
        #   append them to the aforementioned list
        for i in range(2, len(allValues), 2):
            numWeight = eval(allValues[i])
            weights.append(numWeight)
        # This will take the values pertinent for the grades list, and will
        #   append them to the aforementioned list
        for i in range(3, len(allValues), 2):
            numGrade = eval(allValues[i])
            grades.append(numGrade)
        # Using a final loop to index though the grades and weights list
        #   to be able to calculate the weighted average!
        for i in range(len(grades)):
            weightedAverage = ((weights[i]) * (grades[i])) + weightedAverage
        finalWeightAverage = weightedAverage / 100
        weightedAveragesList.append(finalWeightAverage)
        # Output print line for each student
        print(allValues[0] + \" \" + allValues[1] + \"\'s average: {0:.1f}\".format(finalWeightAverage))
    # Calculate the average for the whole class, and output it in a print line
    totalAverageWeighted = ( sum(weightedAveragesList) / len(weightedAveragesList) )
    print(\"\ Class Average: \", (totalAverageWeighted))
    # Close the file
    infile.close()

main()


test.txt


Billy Bother 20 89 30 94 50 82
Hermione Heffalump 40 93 60 97
Kurt Kidd 20 88 30 82 40 76 10 99

Write a Python program: Assignment: A professor at Hardtack University has an unusual method of grading. The students may or may not all take the same number of
Write a Python program: Assignment: A professor at Hardtack University has an unusual method of grading. The students may or may not all take the same number of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site