[Tutor] Bad Operand Type

Michael Schmitt lgwb@no_spam.home.com
Mon, 25 Sep 2000 06:42:47 -0500

I got the same message running the code in the interactive python command
prompt as you did running it in a file.
The error you received "TypeError: bad operand type(s) for /" was indicating
that you can't do division
on a string.

Instead of raw_input use input, as follows:

milesTraveled = input("How many miles did you travel? ")
gallonsUsed = input("How many gallons of gas did you use? ")
pricePerGallon = input("How much did one gallon of gas cost? ")

That way the input will be evaluated and found to be of type floating-point
or integer.

Also, you  probably want the value milesPerGallon to be of type
floating point.  That way your answer will not be truncated at the decimal
point.  This will happen just fine
if the user enters one of the values with a decimal point.  In case they
don't, you will  want to change
the line:

milesPerGallon = milesTraveled / gallonsUsed


milesPerGallon = float(milesTraveled) / gallonsUsed


-----Original Message-----
From: Daniel D. Laskey <dlaskey@laskeycpa.com>
To: 'tutor@python.org' <tutor@python.org>
Date: Sunday, September 24, 2000 10:51 PM
Subject: [Tutor] Bad Operand Type

When I type this program in the python command prompt everthing works well.
When I put it inside the file and try to run it I get the following error

milesTraveled = raw_input("How many miles did you travel? ")
gallonsUsed = raw_input("How many gallons of gas did you use? ")
pricePerGallon = raw_input("How much did one gallon of gas cost? ")

print milesTraveled
print gallonsUsed
print pricePerGallon

# Divide the number of miles by the number of gallons
# used to get MPG.
milesPerGallon = milesTraveled / gallonsUsed
print milesPerGallon

Traceback (innermost last):
  File "travel.py", line 24, in ?
    milesPerGallon = milesTraveled / gallonsUsed
TypeError: bad operand type(s) for /


Tutor maillist  -  Tutor@python.org