# GPA Calculator

Page 1 of 1

## 5 Replies - 11240 Views - Last Post: 04 October 2010 - 07:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=191740&amp;s=ade00a54822da650b2775cbc8f11fa21&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 WTFsandwich

Reputation: 3
• 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: ")
cname2 = raw_input("Second course name: ")
cred2 = raw_input("Second course credit: ")
cname3 = raw_input("Third course name: ")
cred3 = raw_input("Third course credit: ")
cname4 = raw_input("Fourth course name: ")
cred4 = raw_input("Fourth course credit: ")
cname5 = raw_input("Fifth course name: ")
cred5 = raw_input("Fifth course credit: ")

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)
cname2 = cname2.ljust(15)
cred2 = cred2.center(9)
cname3 = cname3.ljust(15)
cred3 = cred3.center(9)
cname4 = cname4.ljust(15)
cred4 = cred4.center(9)
cname5 = cname5.ljust(15)
cred5 = cred5.center(9)

print "                 NUM    LETTER\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

Reputation: 452
• Posts: 797
• 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'

print 'WRONG'
else:
print 'RIGHT'

```

Cheers,
Motoma

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

### #3 WTFsandwich

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

## Re: GPA Calculator

Posted 23 September 2010 - 04:12 PM

Oh I see now. Thanks for the clarification.

### #4 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: GPA Calculator

Posted 23 September 2010 - 04:13 PM

Sure thing, come back if you have any other questions!

### #5 code_m

Reputation: 24
• 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.

### #6 auggiecc87

Reputation: 6
• 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.