【问题标题】:Error: unsupported operand type(s) for /: 'NoneType' and 'int'错误:/ 不支持的操作数类型:“NoneType”和“int”
【发布时间】:2016-03-29 03:36:09
【问题描述】:

我见过几个类似的问题,但没有一个能以正确的方式解决我的问题。这意味着它的回答方式要么不起作用,要么对我来说毫无意义。我会给你代码,这让我很适合和它给出的错误。

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

是错误,代码是:

#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);

【问题讨论】:

    标签: python arrays division operands


    【解决方案1】:

    encrypt 函数没有return 语句,所以它的返回值将是None(没有return 语句的Python 函数的默认返回值),所以None 将被分配给array 的每个元素在runEncrypt() 的外循环的第一次迭代中。这意味着,在外循环的第二次和以后的迭代中,encrypt() 将以(None, j) 作为其参数调用,并且将引发错误,因为程序试图将None 除以一个整数,即未定义。

    要解决这个问题,只需重新定义encrypt,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2023-02-12
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多