[Tutor] TypeError: unsupported operand type(s)
DL Neil
PyTutor at danceswithmice.info
Sun Mar 22 12:36:53 EDT 2020
On 23/03/20 4:10 AM, SATYABRATA DATTA wrote:
> def x1(ap,dp,ph,e1,e2,e3,e4,e5,y):
> if Delta(ap,dp,ph,e1,e2,e3,e4,e5,y) >= 0:
> return 2*(b(ap,dp,ph,e1,e2,e3,e4,e5,y)**3) -
> 9*a*b(ap,dp,ph,e1,e2,e3,e4,e5,y)*c(ap,dp,ph,e1,e2,e3,e4,e5,y) +
> 27*(a**2)*d(ap,dp,ph,e1,e2,e3,e4,e5,y)
> else:
> return None
> def y1(ap,dp,ph,e1,e2,e3,e4,e5,y):
> if Delta(ap,dp,ph,e1,e2,e3,e4,e5,y) >= 0:
> return 3*np.sqrt(3)*a*np.sqrt(Delta(ap,dp,ph,e1,e2,e3,e4,e5,y))
> else:
> return None
>
> This is creating a problem
>
> return
> np.sqrt(x1(ap,dp,ph,e1,e2,e3,e4,e5,y)**2+y1(ap,dp,ph,e1,e2,e3,e4,e5,y)**2)
> TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'
>
>
> I think the main problem is due to the condition for Delta>=0 which returns
> a None value for some input dataset but I only need the Delta>=0 condition
> values
At one time our programming languages gave us no choice but to return
'flags' in some cases and valid-output in others. These days to avoid
the frequent errors experienced working this way, the principle is to
only ever return one type of data - not sometimes a float and sometimes
None (etc).
Be aware that in Python, "exceptions" are not necessarily "errors".
Thus, if the provided-data suits your analysis, there's no problem; but
if it does not meet the criteria, 'exception[al] processing' side-steps
the analysis, perhaps by dropping that data-set, without halting execution.
To solve this problem, if the data does not suit your (current)
analysis, raise a custom exception within the function. In the
'mainline' code, "wrap" the function calls within a try...except block,
thus handling 'good data' situations differently from 'bad data'.
--
Regards =dn
More information about the Tutor
mailing list