Determine miles-per-gallon (Python)


#1

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.


#2

mpg2


#3

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


#4

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.


#5

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


#6

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


#7

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?


#8

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.


#9

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:

https://docs.python.org/2/library/functions.html

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


#10

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


#11

convert the strings to integers

integers are divisible, strings are not.


#12

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