Ext Class

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Ext Class

 
0
  #1
Dec 24th, 2006
This is what I have come up with but it seems like there should be a more simple way of programming it.

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Ext Class

 
0
  #2
Dec 24th, 2006
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.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Ext Class

 
0
  #3
Dec 24th, 2006
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.

  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:

  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

Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Ext Class

 
0
  #4
Dec 27th, 2006
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 1114 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC