TypeError: unsupported operand type(s) for /: 'str' and 'int'

TypeError: unsupported operand type(s) for /: 'str' and 'int'
0

Hi PadhAI Team

I am a novice in coding.

I have been trying out Assignment 1 for Week 3.

Please see the link and attached image.

In one case the code meters = ((feet + inches/12)) * 0.3048 seems to work.

However, when I use the input function the same code returns an error message.

Can someone help me with this and clarify.

Many Thanks

Krishnan

https://colab.research.google.com/drive/1BPRR8eohzNoJBaHcTvV9Kw171CtuYG67

1 Like

Hi @pkkrishnan, the erro is due to the use of input() function. Typecast the input to int or float as the case may be. Hope this helps!!

1 Like

@saag Does the error occur only when we use the input() function?

Hi Krishnan,
The input() function returns a string into your variables. That is, input() literally returns you the raw text that you typed. So when you are using those variables for calculation, you are supposed to convert the variable to the respective datatype.

For example, when you do inches = input("Enter inches"), the inches variable now is of type str (string), which you can see by running type(inches).

Ideally, since you are doing arithmetic calculations with the variable, you can for example convert the inches variable to a floating point number by doing inches = float(inches)

Or as a single step, you can do the following for your variable:
inches = float(input("Enter inches "))
Similarly handle all the variables that you read using input()

Hope this helps.

5 Likes

Thank you so much @GokulNC AND @saag

Bro you are getting error because input() method takes str as input. So, instead of input() try this one int(input()) it will work fine.

2 Likes

try float(input()) method because you are using float in multiplication.

1 Like

Thank you so much @abhisewa

Thank you very much @abhisewa