Thanks for the reply Alan and Noah, I really appreciate the help. I am really trying to understand this although still cannot seem to grasp it all. I have modified my program although now it seems to be giving me the wrong answer with the correct answer when I input any value.
I have program: model=raw_input("What kind of car do you drive?") gallons=raw_input("How many gallons have you driven?") number1 = float (gallons) miles=raw_input("How many miles have you driven?") number2 = float (miles) try: model=float(model) except ValueError: print "I cannot compute your total miles to gallon with those values." else: print "Your average number of miles to gallons is", print number1 / number2 Output: What kind of car do you drive?fire How many gallons have you driven?10 How many miles have you driven?5 I cannot compute your total miles to gallon with those values. On Apr 27, 2011, at 10:02 AM, Alan Gauld wrote: > > "Johnson Tran" <aznj...@me.com> wrote > >> I started out with a short program below and I thought it was working >> although I cannot seem to figure out how to use the except ValueError so >> that when the user puts an invalid answer the program does not read with an >> error. > > You have to replace the line that says 'pass' with code that does something > to make the value correct. Asking the user to try again with a more sensible > value would be a start. > >> Although according to the error message, it seems to be saying that my line >> 4 "number1 = float (number_string1)" is incorrect. > > Its not saying its incorrect, its saying thats where the ValueError occured. > Which is true because you entered 'test' which cannot be converted to a float > - its an invalid value. > >> model=raw_input("What kind of car do you drive?") >> number_string1=raw_input("How many gallons have you driven?") >> number1 = float (number_string1) >> number_string2=raw_input("How many miles have you driven?") >> number2 = float (number_string2) > > You will fin it easier if you use descriptive names for your variables. > > gallons and miles > would seem reasonable here... > >> try: >> model=float(model) >> except ValueError: >> pass > > This says if you get an error ignore it (ie pass). > But you don't want to ignore it, you want to get a valid value. > >> print "Your average number of miles to gallons is", >> print number1 / number2 > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor