Question: For this exercise, we're going to create a class that can read grades and credit from a file and calculates the corresponding GPA. Each line

For this exercise, we're going to create a class that can read grades and credit from a file and calculatesFor this exercise, we're going to create a class that can read the corresponding GPA. Each line in the input file will consist of a letter (A, B, C, D or F), a tab character ("\t"), and a number of credits (a float that is a multiple of 0.5).

Script instructions

  • Call your class Grades.
  • Write an __init__() method with self as the only parameter. This method should initialize an attribute called grades to an empty list. grades will eventually contain two-item tuples where the first item is a letter grade ('A', 'B', 'C', 'D', or 'F') and the second item is a float representing the number of credits that correspond to that grade.
  • Write a read_grades() method with two parameters: self and the path to a file of grades as described above. This method should read each line from the file, split the line into a grade and a string representing the number of credits, convert the credits to a float, and append a tuple consisting of the grade and the credits to the grades attribute of self.
  • Write a calculate_gpa() method with self as the only parameter. Calculate the total number of credits in self.grades and the total value of each grade (the points of the grade times the credits associated with that grade) using the point scale below:

A:4.0 B:3.0 C:2.0 D:1.0 F:0.0

The GPA is the sum of the total value of each grade divided by the total number of credits. Return the GPA.

Outside of your class, write a main() function with one parameter, the path to a file of grades as described above. This function should create an instance of your Grades class and use that instance to read the grades from the specified file, calculate the GPA, and print a message containing the GPA (for example "The GPA is 3.78."). Use an f-string to generate the message you print.

At the end of your script, make an if __name__ == "__main__": statement in which you call the main() function and pass in the first command line argument after the name of your script.

Write script, class, and method docstrings.

When you run your script, specify a file of grades as a command line argument, for example: python3 calculate_gpa.py grades.txt

Download grades.txt (36 Bytes) NOWw OOOO OM - F2 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!