943,937 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1189
  • Python RSS
Dec 24th, 2006
0

Ext Class

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

Python Syntax (Toggle Plain Text)
  1. import string
  2. import math
  3.  
  4. class Student:
  5. def __init__(self, name, hours, qpoints):
  6. self.name = name
  7. self.hours = float(hours)
  8. self.qpoints = float(qpoints)
  9. def getName(self):
  10. return self.name
  11. def getHours(self):
  12. return self.hours
  13. def getQpoints(self):
  14. return self.qpoints
  15. def gpa(self):
  16. return self.qpoints/self.hours
  17. def addLetterGrade(self, letter, credits):
  18. self.hours = credits
  19. if letter == "A":
  20. ngrade = 4.0
  21. elif letter == "A-":
  22. ngrade = 4.0
  23. elif letter == "A+":
  24. ngrade = 4.0
  25. elif letter == "B":
  26. ngrade = 3.0
  27. elif letter == "B-":
  28. ngrade = 3.0
  29. elif letter == "B+":
  30. ngrade = 3.0
  31. elif letter == "C":
  32. ngrade = 2.0
  33. elif letter == "C-":
  34. ngrade = 2.0
  35. elif letter == "C+":
  36. ngrade = 2.0
  37. elif letter == "D":
  38. ngrade = 1.0
  39. elif letter == "D-":
  40. ngrade = 1.0
  41. elif letter == "D+":
  42. ngrade = 1.0
  43. else:
  44. ngrade = 0.0
  45. self.qpoints = credits * ngrade
  46. print self.qpoints, self.hours
  47.  
  48. def main():
  49. print
  50. print "This program extends the modified student class program by"
  51. print "implementing an add letter grade method"
  52. print
  53.  
  54. stu = Student("stu", 0.0, 0.0)
  55.  
  56. done = False
  57. while done == False:
  58. while True:
  59. grade_str = raw_input("Enter grade (A,A-,A+,B,B-,B+,C,C-,C+,"
  60. "D,D-,D+,F or just "
  61. "Enter=exit loop): ").upper()
  62. print
  63. if grade_str == "":
  64. done = True
  65. break
  66. if grade_str not in 'A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F':
  67. print "Error, use A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F"
  68. else:
  69. break
  70. while done == False:
  71. credits_str = raw_input("Enter credits (number of hours): ")
  72. try:
  73. credits = float(credits_str)
  74. break
  75. except ValueError:
  76. print "Error, use floating point number"
  77.  
  78. if done == False:
  79. stu.addLetterGrade(grade_str, credits)
  80.  
  81.  
  82. if stu.getHours() == 0.0:
  83. print "Zero credit hours recorded"
  84. else:
  85. print "Final GPA = ", stu.gpa()
  86.  
  87.  
  88.  
  89. main()

ANY Suggestions?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006
Dec 24th, 2006
0

Re: Ext Class

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]
Last edited by Ene Uran; Dec 24th, 2006 at 2:05 pm.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Dec 24th, 2006
0

Re: Ext Class

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.

Python Syntax (Toggle Plain Text)
  1. if grade_str not in 'A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F':
  2. 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)
  1. import string
  2. import math
  3.  
  4. class Student:
  5. valid_grades = {"A+":4.0, "A":4.0, "A-":4.0, "B+":3.0, "B":3.0, "B-":3.0, \
  6. "C+":2.0, "C":2.0, "C-":2.0, "D+":1.0, "D":1.0 ,"D-":1.0, \
  7. "F":0.0}
  8. valid_symbols = valid_grades.keys()
  9. valid_symbols.sort()
  10.  
  11. def __init__(self, name, hours, qpoints):
  12. self.name = name
  13. self.hours = float(hours)
  14. self.qpoints = float(qpoints)
  15. def getName(self):
  16. return self.name
  17. def getHours(self):
  18. return self.hours
  19. def getQpoints(self):
  20. return self.qpoints
  21. def gpa(self):
  22. return self.qpoints/self.hours
  23. def addLetterGrade(self, letter, credits):
  24. self.hours = credits
  25. if letter in Student.valid_grades:
  26. ngrade = Student.valid_grades[letter]
  27. self.qpoints = credits * ngrade
  28. print self.qpoints, self.hours
  29. else:
  30. print "Error! invalid grade!"
  31.  
  32.  
  33. def main():
  34. print
  35. print "This program extends the modified student class program by"
  36. print "implementing an add letter grade method"
  37. print
  38.  
  39. stu = Student("stu", 0.0, 0.0)
  40.  
  41. done = False
  42. while done == False:
  43. while True:
  44. grade_str = raw_input("Enter grade (" + ','.join(Student.valid_symbols)+\
  45. " or just Enter=exit loop): ").upper()
  46. print
  47. if grade_str == "":
  48. done = True
  49. break
  50. if grade_str not in Student.valid_grades:
  51. print "Error, use "+','.join(Student.valid_symbols)
  52. else:
  53. break
  54. while done == False:
  55. credits_str = raw_input("Enter credits (number of hours): ")
  56. try:
  57. credits = float(credits_str)
  58. break
  59. except ValueError:
  60. print "Error, use floating point number"
  61.  
  62. if done == False:
  63. stu.addLetterGrade(grade_str, credits)
  64.  
  65.  
  66. if stu.getHours() == 0.0:
  67. print "Zero credit hours recorded"
  68. else:
  69. print "Final GPA = ", stu.gpa()
  70.  
  71.  
  72.  
  73. 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

Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Dec 27th, 2006
0

Re: Ext Class

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Trying to get card images into a GUI
Next Thread in Python Forum Timeline: Sorting student info Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC