[Tutor] Just started Python

Edgar Almonte samudhio at gmail.com
Wed Apr 27 23:35:04 CEST 2011


try this :



 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:
number1 = float (gallons)
number2 = float (miles)

 except ValueError:
   print "some values are wrong type."
 else:
   print "Your average number of miles to gallons is",
 print number1 / number2


On Wed, Apr 27, 2011 at 5:11 PM, Noah Hall <enalicho at gmail.com> wrote:
> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list