ValueError - Not Working

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

ValueError - Not Working

 
0
  #1
Jan 7th, 2007
Hi,

I did get the problem fixed with my "break" not working properly but now my ValueError will not work properly. If you enter a number that is not a floating number it should create a value error(as a matter of fact it did before I fixed my other problem ("break"). Can anyone help, please! Thanks!!!



  1. import string
  2. import math
  3. class Student:
  4. def __init__(self, name, hours, qpoints):
  5. self.name = name
  6. self.hours = float(hours)
  7. self.qpoints = float(qpoints)
  8. def getName(self):
  9. return self.name
  10. def getHours(self):
  11. return self.hours
  12. def getQpoints(self):
  13. return self.qpoints
  14. def gpa(self):
  15. return self.qpoints/self.hours
  16. def addGrade(self, gradePoint, credits):
  17. self.hours = credits
  18. self.qpoints = credits*gradePoint
  19. def main():
  20. print "This program is a modified version of the student class. It adds"
  21. print "a mutator method that records a grade and calculates the GPA for"
  22. print "the student"
  23. print
  24.  
  25. stu = Student("stu", 0.0, 0.0)
  26. while 1:
  27. print
  28. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  29. if grade_str == "":
  30. break
  31. try:
  32. grade = float(grade_str)
  33. except ValueError:
  34. print "Error, use floating point number"
  35. return
  36. print
  37. credits_str = raw_input("Enter credit hours or (Enter to quit): ")
  38. if credits_str == "":
  39. break
  40. try:
  41. credits = float(credits_str)
  42. except ValueError:
  43. print "Error, use floating point number"
  44. return
  45. stu.addGrade(grade, credits)
  46.  
  47. if stu.getHours() == 0.0 :
  48. print
  49. print "Zero gradepoints or credit hours recorded"
  50. print
  51. else:
  52. print "Final GPA = ", stu.gpa()
  53. print


Thank You!!
Last edited by babutche; Jan 7th, 2007 at 1:45 pm. Reason: code correction
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,006
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: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: ValueError - Not Working

 
0
  #2
Jan 7th, 2007
The return statement will not only break out of the while loop but also out of function main()!!

If you enter data in a loop you need to accumulate the entries in some fashion.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: ValueError - Not Working

 
0
  #3
Jan 7th, 2007
  1. def main():
  2. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  3. if grade_str == "":
  4. break
  5. try:
  6. grade = float(grade_str)
  7. except ValueError:
  8. print "Error, use floating point number"
  9. return
You want the "try :" statement as the first statement after "while 1:". That will give you an error message if a number is not entered. If an integer is entered, it can legally be type cast into a float. If a float is entered and you try to convert it into an integer you will get an error, so inserting a "grade_int = int(grade_str) and another try:/except: statement will do the trick, but it seems cumbersome. You could also use divmod(grade_float, 1) and check for a remainder. Can someone legimately have a gpa of 70? If so you want to be able to enter an integer. If the gpa is entered as 0.70, then you want to test that the float is less than one.
  1. while 1:
  2. try:
  3. print
  4. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  5. if grade_str == "":
  6. break
  7. grade = float(grade_str)
  8. try :
  9. grade_int = int(grade_str)
  10. print "Error, use floating point number"
  11. except :
  12. print grade
  13. except ValueError:
  14. print "Error, use floating point number"
Edit: Added additional try/except statement. Sorry, I did the first post in a hurry.
Edit-2: I would suggest that you have a GetInput function that you would pass "grade point" or "credit hours" to, it would get the input and convert to floating point and return the number or a -1 if bad data and then exit with an 'enter'. You are doing the same thing twice, so putting this is in a function is more efficient and breaks the code up into smaller chunks, which is easier to debug. Looks like you are just about finished though. Any plans for a Tkinter or other GUI interface?
Last edited by woooee; Jan 7th, 2007 at 10:19 pm.
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: ValueError - Not Working

 
0
  #4
Jan 8th, 2007
Thank You for your help. I had tried to put another "try, except" statement in my program but I didn't do it right. I never really thought about the "int(grade_string)".

Right now I do not have any plans for any Tinker or other GUI interface. But I have done it before. I thought it was very hard and confusing. Of course, when I started the section in the book on classes, I found it even more difficult!! But I am learning with each problem that I finish. I am currently taking an online class to learn Python and I must say that this web site has been a tremendous help! It is like having an instuructor that is online all of the time. You guys (or gals) really know what you're doing and you explain what is going on very well. I really appreciate your time! Thanks again!
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC