[Tutor] User input

Ewald Ertl ewald.ertl at hartter.com
Mon May 22 16:17:42 CEST 2006


MATATA EMMANUEL wrote:
> Hi there,
> 
> Can anyone tell me how I'm having trouble executing this piece of code:
> 
> mpg = raw_input (" Enter your mileage:")
> distance = raw_input ("Enter your distance:")
> galon_price = raw_input ("Enter you cost per gallon:")
> make = "Honda"
> model = "Accord"
> ##
> print make, model, "mileage is", mpg, "miles per gallon"
> print "Total trip cost is US$", distance / (mpg * gallon_price)
> 
> I get this error when I run it:
> 
> print "Total trip cost is US$", distance / (mpg * gallon_price)
> TypeError: unsupported operand type(s) for *
> 
The type which raw_input() returns is a string, so '120'*'5' multiplies
two strings.
Perhaps you can convert your input to a type you need. ( e.g. float ), but you
have to check for a ValueError-Exception if the conversion fails.

HTH Ewald



More information about the Tutor mailing list