PYTHON. calculating a semester GPA
first, code:
#semester GPA calculation
firstName=raw_input("please enter first name:")
lastName=raw_input("please enter last name:")
studentID=raw_input("please enter student ID:")
numClasses=input("how many classes:")
totalHours = 0
totalQP = 0
#running totals--hours and quality points
for i in range(numClasses):
dept=raw_input("department:")
course=raw_input("course:")
hours=input("how many student credit hours:")
grade=raw_input("what grade:")
totalHours = totalHours + hours
if grade =="A":
QP =
4.0
elif grade =="B+":
QP =
3.5
elif grade =="B":
QP = 3.0
elif grade =="C+":
QP = 2.5
elif grade =="C":
QP = 2.0
elif grade =="D":
QP = 1.0
else:
QP = 0.0
totalQP = totalQP + hours*QP
print firstName,lastName,studentID
print "total hours:", totalHours, "quality points:", totalQP
GPA = totalQP/totalHours
print "semester GPA:", GPA
------------------
What we want to do is to input a student's first and last names, and
the student ID. We then want to find out how many courses the
student has taken and for each course, the department name, course,
credit hours, and grade. We then will print out the student info,
the total hours, the total quality points, and the student's GPA.
For the student's ID. This could be numeric or it could be like
UTK (e.g. 012-34-6578). But in any case we are not going to
manipulate this number--we are not multiplying it, adding things to it,
etc. So we'll leave it as a character string. The number of
classes, on the other hand, must be a number--since we must go through
a loop that number of times, once through the loop for each course
taken.
totalHours and totalQP: thse are variables that need to start at
0. When we read in the first course--e.g. Math 201, 4 credit
hours, grade of C+, we need to add 4 to the running total hours.
We need to calculate the quality points earned for each credit hour--4
points for an A, 3.5 for a B+, etc. So in our 4 hour math course,
an A would mean 16 quality points, a B+ would be 14, etc. The
quality points for the course get added to the running total of
QP's.
We do the same thing for each course. NOTE that we are NOT
re-initializing totalHours and total QP each time through the
loop. When we finish up we want the total number of hours--e.g.
10 and the total quality points--e.g. 27. The GPA for the
semester is thus 27/10 which equals a 2.7 GPA.
Prettyprinting. The output will usually show the GPA to lots of
places--more than you want. I haven't found a simple method yet
in Python of controlling this..................