close

[Solved] Type: unsupported operand type(s) for -: ‘float’ and ‘NoneType’ python

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Type: unsupported operand type(s) for -: ‘float’ and ‘NoneType’ python in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Type: unsupported operand type(s) for -: ‘float’ and ‘NoneType’ python Error Occurs?

Today I get the following error Type: unsupported operand type(s) for -: ‘float’ and ‘NoneType’ python in python.

How To Solve Type: unsupported operand type(s) for -: ‘float’ and ‘NoneType’ python Error ?

  1. How To Solve Type: unsupported operand type(s) for -: 'float' and 'NoneType' python Error ?

    To Solve Type: unsupported operand type(s) for -: 'float' and 'NoneType' python Error Note in the last example there is a path where neither if case is satisfied,.. thus it could return None

  2. Type: unsupported operand type(s) for -: 'float' and 'NoneType' python

    To Solve Type: unsupported operand type(s) for -: 'float' and 'NoneType' python Error Note in the last example there is a path where neither if case is satisfied,.. thus it could return None

Solution 1


Judging by what you’ve provided, and the error this is my conclusion.

The only place you use the - operand is in the two points

sq_err = (rating- predicted_rating) * (rating - predicted_rating)

because the error states 'float' and 'NoneType' we can conclude that rating is type float and predicted_rating is NoneType.

You defined predicted_rating as:

predicted_rating = simple_nn(person_id, place_id, 5)

So this means that somewhere in your code for the function simple_nn you are not returning anything. Perhaps if you used conditions you didn’t evaluate every end path and the function simply returned.

for example… all of these functions return the None type.

def example1():
    pass

def example2():
    return

def example3(a = True, b  = True):
    if not a:
        return True
    elif not b:
        return False

Note in the last example there is a path where neither if case is satisfied,.. thus it could return None

Solution 2


you can also use import division

from _ _ future _ _ import division

it worked for me in same case

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read