Brandon Williams
Brandon Williams

Reputation: 79

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

I've seen several questions like this one but none of them solve my issue in the correct way. Meaning the way that it is answered is either not working or makes no sense to me. I'll give you the code thats giving me fits and the error it's giving.

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

Is the error and the code is:

#Define iteration#
iteration=0;
iterationNum=0;

#Define encryption#
def encrypt(num,iteration):
    num=cos(num/(iteration+1));

def runEncrypt(array,iterationNum):
    for j in range(iterationNum):
        for i in range (len(array)):
            array[i]=encrypt(array[i],j);

#Internal test area#
array1=[1,2,3,4,5];
encryptedArray=runEncrypt(array1,4);
print(encryptedArray);

Upvotes: 1

Views: 8697

Answers (1)

mrry
mrry

Reputation: 126164

The encrypt function does not have a return statement, so its return value will be None (the default return value for Python functions without a return statement) and so None will be assigned to every element of array in the first iteration of the outer loop in runEncrypt(). This means that, in the second and later iterations of the outer loop, encrypt() will be called with (None, j) as its arguments, and the error will be raised because the program tries to divide None by an integer, which is undefined.

To resolve this, simply redefine encrypt as follows:

def encrypt(num, iteration):
    return cos(num / (iteration + 1))

Upvotes: 3

Related Questions