This is what I have come up with but it seems like there should be a more simple way of programming it.

``````import string
import math

class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
self.hours = credits
if letter == "A":
elif letter == "A-":
elif letter == "A+":
elif letter == "B":
elif letter == "B-":
elif letter == "B+":
elif letter == "C":
elif letter == "C-":
elif letter == "C+":
elif letter == "D":
elif letter == "D-":
elif letter == "D+":
else:
print self.qpoints, self.hours

def main():
print
print "This program extends the modified student class program by"
print

stu = Student("stu", 0.0, 0.0)

done = False
while done == False:
while True:
"D,D-,D+,F or just "
"Enter=exit loop): ").upper()
print
done = True
break
print "Error, use A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F"
else:
break
while done == False:
credits_str = raw_input("Enter credits (number of hours): ")
try:
credits = float(credits_str)
break
except ValueError:
print "Error, use floating point number"

if done == False:

if stu.getHours() == 0.0:
print "Zero credit hours recorded"
else:
print "Final GPA = ", stu.gpa()

main()``````

ANY Suggestions?

``````s = 'A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F'
ps = """
Enter grade letters A through F, you
can also postfix A through D with + or -
(press just Enter to exit the loop when done): """
while True:
x = raw_input(ps).upper()
print x  # test
if x == "":
done = True
break
if x in s:
break
else:
print "Error, use", s

# .........``````

There is another improvement possible when you assign a value to the grade letters:

``````letter = 'B+'  # test

# the index found is 0 to 4, and -1 if not found

partial = 0.33  # or 0.25 this may vary with the school
if '+' in letter:
if '-' in x:

I would suggest using a dictionary for the grades, for two reasons:

(1) It'll make your life a lot easier (see code below)

(2) Your code currently contains a bug that will allow a silly or malicious user to get weird results or break the code.

``````if grade_str not in 'A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F':
print "Error, use A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F"``````

Note that if the user enters 'A,A-' or '+' or ',' or something similar, the test will pass even though those aren't valid grades! Use of the dictionary will fix that issue.

Generally, when you have constants like "A", "A-", etc... in your program, the philosophy is to only store the constants once. The reason is that if you later decide to change your constants, you won't have to go hunting through your code to find all the instances of that constant. Instead, you can change them once and be done.

Here's one way to implement that principle:

``````import string
import math

class Student:
valid_grades = {"A+":4.0, "A":4.0, "A-":4.0, "B+":3.0, "B":3.0, "B-":3.0, \
"C+":2.0, "C":2.0, "C-":2.0, "D+":1.0, "D":1.0 ,"D-":1.0, \
"F":0.0}
valid_symbols.sort()

def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
self.hours = credits
print self.qpoints, self.hours
else:

def main():
print
print "This program extends the modified student class program by"
print

stu = Student("stu", 0.0, 0.0)

done = False
while done == False:
while True:
" or just Enter=exit loop): ").upper()
print
done = True
break
print "Error, use "+','.join(Student.valid_symbols)
else:
break
while done == False:
credits_str = raw_input("Enter credits (number of hours): ")
try:
credits = float(credits_str)
break
except ValueError:
print "Error, use floating point number"

if done == False:

if stu.getHours() == 0.0:
print "Zero credit hours recorded"
else:
print "Final GPA = ", stu.gpa()

main()``````

The dictionary is now the only place that the grade symbols get defined. If later you wish to change B+ to be 3.33 or 3.25, you can do that with ease and confidence that your code is consistent. Or, if the school decides to allow teachers to award "F+"s, you can add it to the dictionary without having to double-check all your print statements.

And, the endless chain of 'elif's has gone away! :)

You still have one bug that needs attention: it is currently possible for the user to assign negative credit hours to a course.

Hope it helps,
Jeff

Actuall, I never thought about using a dictionary but that makes sense! I will probably not implement this into my program since this is an assignment (and it is not my work) but these answers do help me to see different ways to do things, learn and undertsand. Thanks!

I will work on my negative number problem. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.