[Tutor] TypeError: unsupported operand type(s)

Alan Gauld alan.gauld at yahoo.co.uk
Sun Mar 22 12:31:12 EDT 2020


On 22/03/2020 15:10, 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

> 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

You are correct. So you need to construct the code to do something if
the return value is None (or return some other value)

What should your code do if Delta < 0?

And if delta should not be less than 0 what is making it so?


Alternatively, if you just want to ignore the error you could wrap your
code in a try/except block, but then you run the risk of ignoring other
valid Typeerrors....

Only you can decide what the correct behaviour is!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list