944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3364
  • Python RSS
Jan 7th, 2007
0

ValueError - Not Working

Expand Post »
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!!!



Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006
Jan 7th, 2007
0

Re: ValueError - Not Working

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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 7th, 2007
0

Re: ValueError - Not Working

Python Syntax (Toggle Plain Text)
  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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Jan 8th, 2007
0

Re: ValueError - Not Working

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!
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: Sorting a File
Next Thread in Python Forum Timeline: Dictionary -- key:value pair swap





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


Follow us on Twitter


© 2011 DaniWeb® LLC