Extending a class

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

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

Extending a class

 
0
  #1
Dec 16th, 2006
Hi,

I need help please. At first I was understanding the Python language but I got lost somewhere along the line. Can someone please guide me through implementing an addLetterGrade method? I am not very good with computers and I am having a very hard time. This is my first time using a computer language so, please bare with me. I just need someone to help me and maybe answer some of my questions and explain this to me so I can figure out how to do this. Thanks!!


This is what I have so far:
  1.  
  2. import string
  3. import math
  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.  
  18. ##I AM STUCK HERE
  19. def addLetterGrade(self, Letter, credits):
  20. self.hours = credits
  21. for ch in Letter:
  22. print ord(ch)
  23. self.qpoints = credits*Letter
  24. if grade_str = "A":
  25. grade_str = 4.0
  26. elif grade_str = "B":
  27. grade_str = 3.0
  28. elif grade_str = "C":
  29. grade_str = 2.0
  30. elif grade_str = "D":
  31. grade_str = 1.0
  32. else:
  33. grade_str = 0.0
  34. return
  35.  
  36. def main():
  37. print "This program is a modified version of the student class. It adds"
  38. print "a mutator method that records a grade and calculates the GPA for"
  39. print "the student"
  40. print
  41. print
  42. grade = raw_input("Enter grade for next course, or nothing to finish: ")
  43. print
  44. credits = input("Enter the number of credit hours for this course: ")
  45.  
  46. stu = Student("stu", 0.0, 0.0)
  47. while 1:
  48. grade_str = raw_input("Enter grade: ")
  49. if grade_str == "":
  50. break
  51. try:
  52. grade = float(grade_str)
  53. except ValueError:
  54. print "Error, use floating point number"
  55. return
  56. credits_str = raw_input("Enter credits: ")
  57. try:
  58. credits = float(credits_str)
  59. except ValueError:
  60. print "Error, use floating point number"
  61. return
  62. stu.addGrade(grade, credits)
  63.  
  64. if stu.getHours() == 0.0:
  65. print "Zero credit hours recorded"
  66. else:
  67. print "Final GPA = ", stu.gpa()
  68.  
  69.  
  70.  
  71. main()
Last edited by vegaseat; Dec 16th, 2006 at 12:30 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Extending a class

 
0
  #2
Dec 16th, 2006
At first look, when you compare two items use == in your if statements ...
  1. def addLetterGrade(self, Letter, credits):
  2. self.hours = credits
  3. for ch in Letter:
  4. print ord(ch)
  5. self.qpoints = credits*Letter
  6. if grade_str == "A": # use == !!!!!!!!!!!!
  7. grade_str = 4.0
  8. elif grade_str == "B":
  9. grade_str = 3.0
  10. elif grade_str == "C":
  11. grade_str = 2.0
  12. elif grade_str == "D":
  13. grade_str = 1.0
  14. else:
  15. grade_str = 0.0
  16. return
May 'the Google' be with you!
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: Extending a class

 
0
  #3
Dec 17th, 2006
O.K. This is what I have changed in my program:

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, LetterGrade, credits):
self.hours = credits
self.LetterGrade = Letter
for ch in Letter:
print ord(ch)
self.qpoints = credits*Letter
if Letter == "A":
grade_str = 4.0
elif Letter == "B":
grade_str = 3.0
elif Letter == "C":
grade_str = 2.0
elif Letter == "D":
grade_str = 1.0
else:
grade_str = 0.0
return

def main():
print "This program is a modified version of the student class. It adds"
print "a mutator method that records a grade and calculates the GPA for"
print "the student"
print
print
grade = raw_input("Enter grade for next course, or nothing to finish: ")
print
credits = input("Enter the number of credit hours for this course: ")

stu = Student("stu", 0.0, 0.0)
while 1:
grade_str = raw_input("Enter grade: ")
if grade_str == "":
break
try:
grade = grade_str
except ValueError:
print "Error, use A, B, C, D or F "
return
credits_str = raw_input("Enter credits: ")
try:
credits = float(credits_str)
except ValueError:
print "Error, use floating point number"
return

stu.addLetterGrade(Letter, credits)

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



main()





But it keeps giving me this error:

Enter grade for next course, or nothing to finish: A
Enter the number of credit hours for this course: 3.0
Enter grade: B
Enter credits: 3.0
Traceback (most recent call last):
File "C:\Python23\p6extstclass.py", line 88, in ?
main()
File "C:\Python23\p6extstclass.py", line 78, in main
stu.addLetterGrade(Letter, credits)
NameError: global name 'Letter' is not defined


By the way, thank you for your help so far.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 2335 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC