GPA Python Assignment [closed]

2024/7/7 6:56:26

I have an Python assignment at hand, and I was wondering if you guys can point me in the right direction. I have done the assignments so far up to date, but this one seems to be a bit more advanced and I have a midterm on Friday that will have elements of this program in it, and I'd love for you guys to add any insight you may think that can be beneficial in regards to this program. Thank you.

"The input will be a text grade file, which I called grade_file.py in my case, so I could edit it with the IDLE editor, in a form as shown below.

COURSE A 4
COURSE B 3
COURSE C 5
COURSE D 5

Note that each line has three pieces of information, separated by spaces. The first is the name of the course, the second is the letter grade, and the third is the number of units. In order to make the computation of the numerical value for the grade a simple formula in the letter grade, the only legal values or the letter grades are A, B, C, D, and E, with the numerical point values

A  4
B  3
C  2
D  1
E  0

so that E is like our F, and no + or - can be added after the letter.

The total numerical points are calculated by adding up the products of the the units time the grade's numerical point value for for all the courses, and then dividing by the total number of units.

Write a program that can prompt the user for the name of the grade file, and compute the GPA from the data in the file. The GPA should be printed out with exactly 3 places to the right of the decimal point. Below are the results of my program on the input data above, with the user input in bold. (Note that the grader will use a different grade data file, with a different name.)

Enter the name of the file of grades: grade_file.py The GPA is 2.353"

Thanks again guys.

Answer

Edit: Much nicer version (also fixed req. to force float division in 2.7.x etc..):

from re import findall
GPAG = {"A":4,"B":3,"C":2,"D":1,"E":0}def calcGPA(ResultStr):courses = [ dict(zip(["grade","units"],score)) for score in findall(r'COURSE\s(\w)\s(\d+)',ResultStr) ]print(courses)unitCount = sum([int(course["units"]) for course in courses])return sum([GPAG[course["grade"]]*int(course["units"]) for course in courses] )/float(unitCount)fname = raw_input("Enter the name of the file of grades: ")
with open(fname+".txt") as f:print("The GPA is: %.3f" % calcGPA(f.read()))

With file:

COURSE A 4
COURSE B 3
COURSE C 5
COURSE D 5

Produces:

>>> 
Enter the name of the file of grades: GPA
The GPA is: 2.353
https://en.xdnf.cn/q/120456.html

Related Q&A

Tuple Errors Python

I opened Python and attempted to run the following script (btw, this script is what was directly given to me, I havent edited it in any way as its part of an assignment aside from entering the username…

Flatten list of lists within dictionary values before processing in Pandas

Issue:If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:[item for sublist in l for item in sublist]I have a dictionary where some of the va…

how to analyse and predict(machine learning) a time series data set using scikit-learn for python

i got data-set like this i need to analyse and predict the status column. This is just 2 entrees from the training data set. In this data set there is heart rate pattern(which is collected in 1 second …

Datetime - Strftime and Strptime

Date = datetime.datetime.today().strftime("%d %B %Y") x = datetime.datetime.strptime(Date , "%d %B %Y")returns:2018-05-09 00:00:00instead of: 9 May 2018, what am I doing wrong? Ess…

Subset sum overlapping dilemma recursively

Im studying recursive logic that one of the problems is subset sum. AFAI read, there are overlaps when it is run by recursion. But, I cannot figure out how there are. Also, I read that to overcome the …

Python - download video from indirect url

I have a link like thishttps://r9---sn-4g57knle.googlevideo.com/videoplayback?id=10bc30daeba89d81&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-4g57knle&ms=nxu&a…

Python trading logic

Heres a simple code for downloading daily stock data and computing Bollinger band indicator, but what I am not able to do is set up a logic for generating a buy and sell signal. Can someone help me wit…

Convert PDF to Excel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Return the furthermost outlier in kmeans clustering? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Highest number of consecutively repeating values in a list

Lets say I have this list:List= [1,1,1,0,0,1,1,1,1,1]How do I display the highest number of repeating 1s in a row? I want to return 5.