| | |
ValueError - Not Working
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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!!!
Thank You!!
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)
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 addGrade(self, gradePoint, credits): self.hours = credits self.qpoints = credits*gradePoint 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" stu = Student("stu", 0.0, 0.0) while 1: grade_str = raw_input("Enter gradepoint or (Enter to quit): ") if grade_str == "": break try: grade = float(grade_str) except ValueError: print "Error, use floating point number" return credits_str = raw_input("Enter credit hours or (Enter to quit): ") if credits_str == "": break try: credits = float(credits_str) except ValueError: print "Error, use floating point number" return stu.addGrade(grade, credits) if stu.getHours() == 0.0 : print "Zero gradepoints or credit hours recorded" else: print "Final GPA = ", stu.gpa()
Thank You!!
Last edited by babutche; Jan 7th, 2007 at 1:45 pm. Reason: code correction
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
Python Syntax (Toggle Plain Text)
def main(): grade_str = raw_input("Enter gradepoint or (Enter to quit): ") if grade_str == "": break try: grade = float(grade_str) except ValueError: print "Error, use floating point number" return
Python Syntax (Toggle Plain Text)
while 1: try: grade_str = raw_input("Enter gradepoint or (Enter to quit): ") if grade_str == "": break grade = float(grade_str) try : grade_int = int(grade_str) print "Error, use floating point number" except : print grade except ValueError: print "Error, use floating point number"
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.
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Flash Player stop working since Ad-aware (Web Browsers)
- Internet Explorer not working. (Web Browsers)
- Flash Player stop working and won't reinstall (Windows NT / 2000 / XP)
- messanger not working. (Web Browsers)
- cd burner not working (Storage)
- Working on new design (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: Sorting a File
- Next Thread: Dictionary -- key:value pair swap
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character cmd code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics string strings sudokusolver sum table terminal text textarea thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wikipedia write wxpython xlib






