I got the below error:
unsupported operand type(s) for /: 'list' and 'int'
How do I solve this problem? Any idea?
Here is my code:
def func(xdata_1,cc,dd,gg):
return cc*(xdata_1**(dd))*
(10**(-1.572*gg*( (185/((xdata_1/420)**2 + (420/xdata_1)**2 + 90 )) )
params,pcov = curve_fit(func,xdata_1,ydata_1,
sigma=err_1, absolute_sigma=True)
fc_1 = func(xdata_1, *params)
The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .
Unsupported operand type(s) for +: 'NoneType' and 'int' # The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.
The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .
Check data type of all variable i.e. xdata_1
,cc
,dd
,gg
1. How to check type of variable:
Use 'type` inbuilt function to get type of variable.
Demo:
>>> d
[1, 2, 3]
>>> type(d)
<type 'list'>
>>>
2. About Exception:
This exception come when we operate /
operation on list
and int
variables.
Demo:
>>> d = [1,2,3]
>>> d/4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'int'
>>>
3. Give input:
Best to provide input details in the question i.e. value of xdata_1
and params
, so we can give you where code is wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With