GPA Calculator

Incorrect results, not sure why.

Page 1 of 1

5 Replies - 8683 Views - Last Post: 04 October 2010 - 07:08 AM Rate Topic: -----

#1 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 02-December 09

GPA Calculator

Posted 23 September 2010 - 03:35 PM

For one of my homework assignments in my intro Python class, I have to create a program that will calculate a GPA, given the number of credits for a course and the credits received.

For some reason, calculating the overall GPA always gives a result of 4.00.

The process I was told to use to calculate the GPA is

((Credit Hours of Course 1 * Credits Obtained in Course 1) + (Credit Hours of Course 2 * Credits Obtained in Course 2) + ...)/Total Credit Hours

I'm not sure what's wrong with my code that's preventing the GPA from not being 4.00.

Here's what I have so far:
from __future__ import division
import string

print "This program will calculate a Semester GPA for a given set of courses\n"
cname1 = raw_input("First course name: ")
cred1 = raw_input("First course credit: ")
grade1 = raw_input("First course grade: ")
cname2 = raw_input("Second course name: ")
cred2 = raw_input("Second course credit: ")
grade2 = raw_input("Second course grade: ")
cname3 = raw_input("Third course name: ")
cred3 = raw_input("Third course credit: ")
grade3 = raw_input("Third course grade: ")
cname4 = raw_input("Fourth course name: ")
cred4 = raw_input("Fourth course credit: ")
grade4 = raw_input("Fourth course grade: ")
cname5 = raw_input("Fifth course name: ")
cred5 = raw_input("Fifth course credit: ")
grade5 = raw_input("Fifth course grade: ")

totalGP = 0.0
overallGPA = 0.0

cred1i = string.atoi(cred1)
cred2i = string.atoi(cred2)
cred3i = string.atoi(cred3)
cred4i = string.atoi(cred4)
cred5i = string.atoi(cred5)

if grade1 == "A" or "a":
    c1points = (4.0*cred1i)
elif grade1 == "B" or "b":
    c1points = (3.0*cred1i)
elif grade1 == "C" or "c":
    c1points = (2.0*cred1i)
elif grade1 == "D" or "d":
    c1points = (1.0*cred1i)
else:
    c1points = 0.0

if grade2 == "A" or "a":
    c2points = (4.0*cred2i)
elif grade2 == "B" or "b":
    c2points = (3.0*cred2i)
elif grade2 == "C" or "c":
    c2points = (2.0*cred2i)
elif grade2 == "D" or "d":
    c2points = (1.0*cred2i)
else:
    c2points = 0.0

if grade3 == "A" or "a":
    c3points = (4.0*cred3i)
elif grade3 == "B" or "b":
    c3points = (3.0*cred3i)
elif grade3 == "C" or "c":
    c3points = (2.0*cred3i)
elif grade3 == "D" or "d":
    c3points = (1.0*cred3i)
else:
    c3points = 0.0

if grade4 == "A" or "a":
    c4points = (4.0*cred4i)
elif grade4 == "B" or "b":
    c4points = (3.0*cred4i)
elif grade4 == "C" or "c":
    c4points = (2.0*cred4i)
elif grade4 == "D" or "d":
    c4points = (1.0*cred4i)
else:
    c4points = 0.0

if grade5 == "A" or "a":
    c5points = (4.0*cred5i)
elif grade5 == "B" or "b":
    c5points = (3.0*cred5i)
elif grade5 == "C" or "c":
    c5points = (2.0*cred5i)
elif grade5 == "D" or "d":
    c5points = (1.0*cred5i)
else:
    c5points = 0.0

totalCredits = cred1i+cred2i+cred3i+cred4i+cred5i
overallGPA = 0.0
overallGPA = (c1points + c2points + c3points + c4points + c5points)/totalCredits

cname1 = cname1.ljust(15)
cred1 = cred1.center(9)
grade1 = grade1.center(6)
cname2 = cname2.ljust(15)
cred2 = cred2.center(9)
grade2 = grade2.center(6)
cname3 = cname3.ljust(15)
cred3 = cred3.center(9)
grade3 = grade3.center(6)
cname4 = cname4.ljust(15)
cred4 = cred4.center(9)
grade4 = grade4.center(6)
cname5 = cname5.ljust(15)
cred5 = cred5.center(9)
grade5 = grade5.center(6)

print "                 NUM    LETTER\n"
print "COURSE         CREDITS  GRADE \n" 
print "------         -------  ----- \n" 
print '%s%s%s' % (cname1, cred1, grade1)
print '%s%s%s' % (cname2, cred2, grade2)
print '%s%s%s' % (cname3, cred3, grade3)
print '%s%s%s' % (cname4, cred4, grade4)
print '%s%s%s' % (cname5, cred5, grade5)
print "SEMESTER GPA = %.2f" % (overallGPA)



Is This A Good Question/Topic? 0
  • +

Replies To: GPA Calculator

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: GPA Calculator

Posted 23 September 2010 - 03:56 PM

Ah, you've made a small error with your if statements. Hint: 'a' equates to True.

Another hint, try this in your Python interpreter:
grade = 'b'

if grade == 'A' or 'a':
    print 'WRONG'
else:
    print 'RIGHT'



It should be written like this:
grade = 'b'

if grade == 'A' or grade == 'a':
    print 'WRONG'
else:
    print 'RIGHT'



Cheers,
Motoma

This post has been edited by Motoma: 23 September 2010 - 03:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 02-December 09

Re: GPA Calculator

Posted 23 September 2010 - 04:12 PM

Oh I see now. Thanks for the clarification.
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: GPA Calculator

Posted 23 September 2010 - 04:13 PM

Sure thing, come back if you have any other questions!
Was This Post Helpful? 0
  • +
  • -

#5 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: GPA Calculator

Posted 02 October 2010 - 12:13 PM

Python also has a great membership operator. The in keyword tests for membership when used inside of if statememts. So you can do this:

if grade in {"A", "a"}:


The above code checks if the string grade is equal to any member inside of the container (a set). This becomes extremely useful when your mixing data types, but since you are not you can also do:

if grade.lower() == "a":


Which will force the contents of grade to be in lowercase before making the comparison.
Was This Post Helpful? 0
  • +
  • -

#6 auggiecc87  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 09-March 09

Re: GPA Calculator

Posted 04 October 2010 - 07:08 AM

You might consider making lists of the credits and the grade for each class.

Then using a loop over the c[1-5]points section so that section of code is reused rather than retyped.

Just a thought to clear it up/make better usage of the code written.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1