Typeerror: Unsupported Operand Type(S) for -: 'Str' and 'Int'

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

n is string. So you need to change it to int:

n = int(n)

If you input [5,6,2,7] on line 37, python interpret it as string like "[5,6,2,7]".
So, you need to convert string to list.

arr = eval(arr)

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

The error is generating in i[2]/1024 this snippet of code, where the i[2] is actually interpreted as a string and 1024 as an int. That's why the error is there.

You have to convert the string into a number

To convert the hex string into a decimal number, use int(i[2], 16)

To convert the hex string into a decimal number, use bin(int(i[2], 16))

Use your preferred type of conversion and use it. I hope that'll solve your issue.

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

You will need to cast the inputs to int() where you explicitly expect them to be integers. All input captured from the terminal comes across as string. Attempting to add strings just concatenates them. Division is not supported on strings hence this error that / is an invalid operand for type str.

I am getting this error unsupported operand type(s) for ** or pow(): 'str' and 'int'

input() returns a string, which has to be converted into a number.

So like

height= int(input("Enter Height in meter"))
weight= int(input (" Enter Weight in Kg "))
result = weight/(height**2)
print(result)

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

Please have a look at your input csv. If there is any line not containing a number, pandas read_csv will interpret each cell as a string. Therefore, your matrixes will not contain numbers, but strings, which will cause the error you observed. Using
pd.read_csv('hw03_problem2.csv', skipinitialspace=True, dtype=int)
will inform pandas, that you expect integers (replace with whatever numerical format your csv contains). However this will still fail, if there are cells inside your csv that contain text

AFTER OP HAS EDITED HIS QUESTION

your trainingYdf columns will still contain matrixes of strings, since np.matrix does not convert to numerical values.

tmp = [["1","2","3"],["4","5","6"]]  
x = np.matrix(tmp)
print(x)

will result in

[['1' '2' '3']
['4' '5' '6']]

Try to convert each entry in your 'trainingYdf' before casting to np.matrix (assuming that its single dimensional)

trainingYdf = [float(x) for x in trainingYdf]

Facing this error :- TypeError: unsupported operand type(s) for -: 'str' and 'float'?

I`m not sure what you think this is doing, but it's not what you expect:

B = dict((literal_eval, i) for i in dict_A.items()) #convert k, v to int

That doesn't convert anything to int. The inner part of that:

(literal_eval, i)

will product a tuple that contains the literal_eval function object, and the item pairs from your dictionary. I'm guessing you didn't print(B) to see what you were building. Since all of the items have the same key, what you end up with is this:

>>> B = dict((literal_eval, i) for i in t.items()) #convert k, v to int
>>> B
{<function literal_eval at 0x7f531ddcb5e0>: ('4', '[3,5]')}

What you actually want is to CALL literal_eval:

>>> B = dict((literal_eval(k), literal_eval(v)) for k,v in t.items())
>>> B
{1: [0, 0], 2: [5, 0], 3: [6, 0], 4: [3, 5]}
>>>


Related Topics



Leave a reply



Submit