[Tutor] Just started Python

Noah Hall enalicho at gmail.com
Wed Apr 27 23:11:00 CEST 2011


On Wed, Apr 27, 2011 at 9:17 PM, Johnson Tran <aznjonn at me.com> wrote:
> 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.

Again, it's because you're "validating" the wrong variable. model is
_not_ what you want to validate - it needs to be a string! gallons and
miles are what you want to validate. So try moving your try: except:
somewhere else, where it will validate the gallons and miles.


More information about the Tutor mailing list