Determine miles-per-gallon (Python)


Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output

TypeError: unsupported operand type (s) for /: ‘str’ and ‘str’

Hello. I am new to computer programming and I am trying to figure out why im getting this error. I am trying to caclulate miles driven divided by gallons of gas used. I took screen shots of my work.




what data types are milesDriven and gallonsUsed? And is it permitted to use division on this data type?


I’m assuming integers. I want to divide the miles driven by gallons of gas used. I apologize if i don’t understand right away. its my second day. I hope everyone can see both screen shots I took.


And why do you assume they are of type integer? Do you think there is a way to verify if those variables are of type integer?

judging by the error message, those variables are not of type int


I’m not sure anymore. That is why I came here in hoping to get in answer or some direction.


python has a built-in type() function which allows us to check data-type. For example:

type("hello world")

would tell me that the data type is string

I already gave a bit of a hint that those variables are not of type integer, see the str (short for string) in the error message?


Yes. So both of them are strings and I need to convert them to numbers in order to calculate the division problem but thats where I am stuck.


if you said that in the beginning, i could have helped you with that :wink:

you could google it? Python convert string to integer

or check the documentation:

see if there is a built-in function to do this


:smiley: Well, what do you suggest I do to fix this error?


convert the strings to integers

integers are divisible, strings are not.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.