| | |
Ext Class
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
This is what I have come up with but it seems like there should be a more simple way of programming it.
ANY Suggestions?
Python Syntax (Toggle Plain Text)
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 def addLetterGrade(self, letter, credits): self.hours = credits if letter == "A": ngrade = 4.0 elif letter == "A-": ngrade = 4.0 elif letter == "A+": ngrade = 4.0 elif letter == "B": ngrade = 3.0 elif letter == "B-": ngrade = 3.0 elif letter == "B+": ngrade = 3.0 elif letter == "C": ngrade = 2.0 elif letter == "C-": ngrade = 2.0 elif letter == "C+": ngrade = 2.0 elif letter == "D": ngrade = 1.0 elif letter == "D-": ngrade = 1.0 elif letter == "D+": ngrade = 1.0 else: ngrade = 0.0 self.qpoints = credits * ngrade print self.qpoints, self.hours def main(): print "This program extends the modified student class program by" print "implementing an add letter grade method" stu = Student("stu", 0.0, 0.0) done = False while done == False: while True: grade_str = raw_input("Enter grade (A,A-,A+,B,B-,B+,C,C-,C+," "D,D-,D+,F or just " "Enter=exit loop): ").upper() if grade_str == "": done = True break 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" 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: stu.addLetterGrade(grade_str, credits) if stu.getHours() == 0.0: print "Zero credit hours recorded" else: print "Final GPA = ", stu.gpa() main()
ANY Suggestions?
Your error trapping in the letter grades can be mildly improved:
[php]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
# .........
[/php]There is another improvement possible when you assign a value to the grade letters:
[php]letter = 'B+' # test
# the index found is 0 to 4, and -1 if not found
ngrade = "FDCBA".find(letter[0])
partial = 0.33 # or 0.25 this may vary with the school
if '+' in letter:
ngrade = ngrade + partial
if '-' in x:
ngrade = ngrade - partial
print ngrade # test
[/php]
[php]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
# .........
[/php]There is another improvement possible when you assign a value to the grade letters:
[php]letter = 'B+' # test
# the index found is 0 to 4, and -1 if not found
ngrade = "FDCBA".find(letter[0])
partial = 0.33 # or 0.25 this may vary with the school
if '+' in letter:
ngrade = ngrade + partial
if '-' in x:
ngrade = ngrade - partial
print ngrade # test
[/php]
Last edited by Ene Uran; Dec 24th, 2006 at 2:05 pm.
drink her pretty
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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.
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:
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
(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.
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
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 = valid_grades.keys() 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 def addLetterGrade(self, letter, credits): self.hours = credits if letter in Student.valid_grades: ngrade = Student.valid_grades[letter] self.qpoints = credits * ngrade print self.qpoints, self.hours else: print "Error! invalid grade!" def main(): print "This program extends the modified student class program by" print "implementing an add letter grade method" stu = Student("stu", 0.0, 0.0) done = False while done == False: while True: grade_str = raw_input("Enter grade (" + ','.join(Student.valid_symbols)+\ " or just Enter=exit loop): ").upper() if grade_str == "": done = True break if grade_str not in Student.valid_grades: 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: stu.addLetterGrade(grade_str, credits) if stu.getHours() == 0.0: print "Zero credit hours recorded" else: print "Final GPA = ", stu.gpa() main()
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
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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!
I will work on my negative number problem. Thank you!
![]() |
Similar Threads
- download and upload (ASP.NET)
- Cannot get to Internet Explorer (Viruses, Spyware and other Nasties)
- CommonNames problem (Web Browsers)
- my internet cache won't clear... help! (Web Browsers)
- help (Web Browsers)
- first math class in uni (Geeks' Lounge)
Other Threads in the Python Forum
- Previous Thread: Trying to get card images into a GUI
- Next Thread: Sorting student info Problem
Views: 1114 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced assignment beginner bluetooth chmod cmd code convert count csv data decimals dictionary dynamic dynamically enter examples excel external file float format frange ftp function gnu gui heads hints homework import input jaunty java leftmouse line linux list lists loan loop module mouse multiple newb number numbers output parsing path pointer port prime program programming projects push py2exe pygame pyglet pyopengl pyqt python random raw_input recursion recursive scrolledtext slicenotation software ssh stderr string strings table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial ubuntu unicode unix urllib urllib2 variable variables ventrilo web webservice windows wx.wizard wxpython xlib






